代码如下:
- // 用 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
- Author: $author
- Publisher: $publisher
- ;
- }
- /*
- 脚本首先创建一个 new DOMdocument 对象, 用 load 方法把图书 XML 装入这个对象之后, 脚本 用 getElementsByName 方法得到指定名称下的所有元素的列表
- 在 book 节点的循环中, 脚本用 getElementsByName 方法获得 author publisher 和 title 标记的 nodeValuenodeValue 是节点中的文本脚本然后显示这些值
- */
代码如下:
- // 用 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'].;
- }
- /**/
脚本首先设置 g_books 数组, 它在内存中容纳所有图书和图书信息, g_elem 变量保存脚本目前正在处理的标记的名称然后脚 本定义回调函数在这个示例中, 回调函数是 startElementendElement 和 textData 在打开和关闭标记的时候, 分别调 用 startElement 和 endElement 函数在开始和结束标记之间的文本上面, 调用 textData
在这个示例中, startElement 标记查找 book 标记, 在 book 数组中开始一个新元素然 后, textData 函数查看当前元素, 看它是不是 publishertitle 或 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\>/s, $xml, $bookblocks );
- foreach( $bookblocks[1] as $block ){
- preg_match_all( /\(.*?)\<\/author\>/, $block, $author );
- preg_match_all( /\(.*?)\<\/title\>/, $block, $title );
- preg_match_all( /\(.*?)\<\/publisher\>/, $block, $publisher );
- echo( $title[1][0]. - .$author[1][0]. - . $publisher[1][0]. );
- }
- /*
我从不建议使用正则表达式读取 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 添加到它然后脚本为每本书 的 authortitle 和 publisher 创建节点, 并为每个节点添加文本节点每个 book 节点的最后一步是重新把它添加到根节 点 books
使用 DOM 的真正价值在于它创建的 XML 总是格式正确的但是如果不能用 DOM 创建 XML 时该怎么办?
Xml 代码
代码如下:
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
- );
- ?>
- foreach( $books as $book )
- {
- ?>
- }
- ?>
实例中用到的 test.xml 如下:
代码如下:
- Jack Herrington
- O'Reilly
- Jack Herrington
- O'Reilly
来源: https://www.php1.cn/detail/php-bda028d107.html