用PHP实现读取和编写XML DOM代码

复制代码 代码如下:

  // 用 DOM 读取 XML

  $doc = new DOMDocument();

  $doc->load(‘test.xml');

  $books = $doc->getElementsByTagName(“book”);

  foreach( $books as $book ){

  $authors = $book->getElementsByTagName(“author”);

  $author = $authors->item(0)->nodeValue; // nodeValue属 性可根据节点的类型来设置或返回某个节点的值。

  $publishers = $book->getElementsByTagName(“publisher”);

  $publisher = $publishers->item(0)->nodeValue;

  $titles = $book->getElementsByTagName( ”title” );

  $title = $titles->item(0)->nodeValue;

  echo ”Title: $title <br> Author: $author <br> Publisher: $publisher<br><hr><br>”;

  }

  /*

  脚本首先创建一个 new DOMdocument 对象,用 load 方法把图书 XML 装入这个对象。之后,脚本 用 getElementsByName 方法得到指定名称下的所有元素的列表。

  在 book 节点的循环中,脚本用 getElementsByName 方法获得 author、 publisher 和 title 标记的 nodeValue。nodeValue 是节点中的文本。脚本然后显示这些值。

  */

  

复制代码 代码如下:

  // 用 SAX 解析器读取 XML

  $g_books = array();

  $g_elem = null;

  function startElement( $parser, $name, $attrs ){

  global $g_books, $g_elem;

  if ( $name == 'BOOK' ) $g_books []= array();

  $g_elem = $name;

  }

  function endElement( $parser, $name ){

  global $g_elem;

  $g_elem = null;

  }

  function textData( $parser, $text ){

  global $g_books, $g_elem;

  if ( $g_elem == 'AUTHOR' || $g_elem == 'PUBLISHER' || $g_elem == 'TITLE' ){

  $g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text;

  }

  }

  $parser = xml_parser_create();

  xml_set_element_handler( $parser, ”startElement”, ”endElement” );

  xml_set_character_data_handler( $parser, ”textData” );

  $f = fopen( 'test.xml', 'r' );

  while( $data = fread( $f, 4096 ) ){

  xml_parse( $parser, $data );

  }

  xml_parser_free( $parser );

  foreach( $g_books as $book ){

  echo $book['TITLE'].” - ”.$book['AUTHOR'].” - ”;

  echo $book['PUBLISHER'].”\n”;

  }

  /*

  脚本首先设置 g_books 数组,它在内存中容纳所有图书和图书信息,g_elem 变量保存脚本目前正在处理的标记的名称。然后脚 本定义回调函数。在这个示例中,回调函数是 startElement、endElement 和 textData。在打开和关闭标记的时候,分别调 用 startElement 和 endElement 函数。在开始和结束标记之间的文本上面,调用 textData。

  在这个示例中,startElement 标记查找 book 标记,在 book 数组中开始一个新元素。然 后,textData 函数查看当前元素,看它是不是 publisher、title 或 author 标记。如果是,函数就把当前文本放入当前图 书。

  为了让解析继续,脚本用 xml_parser_create 函数创建解析器。然后,设置回调句柄。之后,脚本读取文件并把文件的大块 内容发送到解析器。在文件读取之后,xml_parser_free 函数删除解析器。脚本的末尾输出 g_books 数组的内容。

  */

  // 用正则表达式解析 XML

  

复制代码 代码如下:

  $xml = ”";

  $f = fopen( 'test.xml', 'r' );

  while( $data = fread( $f, 4096 ) ) { $xml .= $data; }

  fclose( $f );

  preg_match_all( ”/\<book\>(.*?)\<\/book\>/s”, $xml, $bookblocks );

  foreach( $bookblocks[1] as $block ){

  preg_match_all( ”/\<author\>(.*?)\<\/author\>/”, $block, $author );

  preg_match_all( ”/\<title\>(.*?)\<\/title\>/”, $block, $title );

  preg_match_all( ”/\<publisher\>(.*?)\<\/publisher\>/”, $block, $publisher );

  echo( $title[1][0].” - ”.$author[1][0].” - ”. $publisher[1][0].”\n” );

  }

  /*

  我从不建议使用正则表达式读取 XML,但是有时它是兼容性最好的方式,因为正则表达式函数总是可用的。不要用正则表达式读取直接来自用户 的 XML,因为无法控制这类 XML 的格式或结构。应当一直用 DOM 库或 SAX 解析器读取来自用户的 XML。

  */

  // 用 DOM 编写 XML

  

复制代码 代码如下:

  $books = array();

  $books [] = array(

  'title' => 'PHP Hacks',

  'author' => 'Jack Herrington',

  'publisher' => ”O'Reilly”

  );

  $books [] = array(

  'title' => 'Podcasting Hacks',

  'author' => 'Jack Herrington',

  'publisher' => ”O'Reilly”

  );

  $doc = new DOMDocument();

  $doc->formatOutput = true;

  $r = $doc->createElement( ”books” );

  $doc->appendChild( $r );

  foreach( $books as $book ){

  $b = $doc->createElement( ”book” );

  $author = $doc->createElement( ”author” );

  $author->appendChild( $doc->createTextNode( $book['author'] ) );

  $b->appendChild( $author );

  $title = $doc->createElement( ”title” );

  $title->appendChild( $doc->createTextNode( $book['title'] ) );

  $b->appendChild( $title );

  $publisher = $doc->createElement( ”publisher” );

  $publisher->appendChild( $doc->createTextNode( $book['publisher'] ) );

  $b->appendChild( $publisher );

  $r->appendChild( $b );

  }

  //echo $doc->saveXML();

  /*

  在脚本的顶部,用一些示例图书装入了 books 数组。这个数据可以来自用户也可以来自数据库。

  示例图书装入之后,脚本创建一个 new DOMDocument,并把根节点 books 添加到它。然后脚本为每本书 的 author、title 和 publisher 创建节点,并为每个节点添加文本节点。每个 book 节点的最后一步是重新把它添加到根节 点 books。

  使用 DOM 的真正价值在于它创建的 XML 总是格式正确的。但是如果不能用 DOM 创建 XML 时该怎么办?

  Xml代码

  

复制代码 代码如下:

  <?php

  PHP 编写xml

  $books = array();

  $books [] = array(

  'title' => 'PHP Hacks',

  'author' => 'Jack Herrington',

  'publisher' => ”O'Reilly”

  );

  $books [] = array(

  'title' => 'Podcasting Hacks',

  'author' => 'Jack Herrington',

  'publisher' => ”O'Reilly”

  );

  ?>

  <books>

  <?php

  foreach( $books as $book )

  {

  ?>

  <book>

  <title><?php echo( $book['title'] ); ?></title>

  <author><?php echo( $book['author'] ); ?>

  </author>

  <publisher><?php echo( $book['publisher'] ); ?>

  </publisher>

  </book>

  <?php

  }

  ?>

  </books>

  实例中用到的 test.xml 如下:

  

复制代码 代码如下:

  <?xml version=”1.0″ encoding=”utf8″?>

  <books>

  <book>

  <author>Jack Herrington</author>

  <title>PHP Hacks</title>

  <publisher>O'Reilly</publisher>

  </book>

  <book>

  <author>Jack Herrington</author>

  <title>Podcasting Hacks</title>

  <publisher>O'Reilly</publisher>

  </book>

  </books>