用 PHP 读取和编写 XML DOM

简单代码:

 

Php代码   收藏代码
  1. <?php    
  2. $str = file_get_contents('test.xml');    
  3. //如果不是utf8的编码    
  4. //$xml = simplexml_load_string(iconv('gb2312', 'utf-8', $str));    
  5. //是utf-8的    
  6. $xml = simplexml_load_string($str);    
  7. foreach($xml->book as $sfile)    
  8. {    
  9.     echo $sfile->author . "<br/>";    
  10.     echo $sfile->title . "<br/>";    
  11.     echo $sfile->publisher . "<br/>";    
  12. }    
 

XML:

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf8"?>  
  2.   <books>  
  3.       <book>  
  4.           <author>Jack Herrington</author>  
  5.           <title>PHP Hacks</title>  
  6.           <publisher>O'Reilly</publisher>  
  7.       </book>  
  8.   
  9.       <book>  
  10.           <author>Jack Herrington</author>  
  11.           <title>Podcasting Hacks</title>  
  12.           <publisher>O'Reilly</publisher>  
  13.       </book>  
  14.   </books>  
 

读取:

 

Php代码   收藏代码
  1. // 用 DOM 读取 XML  
  2.     $doc = new DOMDocument();  
  3.     $doc->load('test.xml');  
  4.         
  5.     $books = $doc->getElementsByTagName("book");  
  6.   
  7.     foreach$books as $book ){  
  8.         $authors = $book->getElementsByTagName("author");  
  9.         $author = $authors->item(0)->nodeValue;  // nodeValue属性可根据节点的类型来设置或返回某个节点的值。  
  10.             
  11.         $publishers = $book->getElementsByTagName("publisher");  
  12.         $publisher = $publishers->item(0)->nodeValue;  
  13.             
  14.         $titles = $book->getElementsByTagName( "title" );  
  15.         $title = $titles->item(0)->nodeValue;  
  16.             
  17.         echo "Title: $title <br> Author: $author <br> Publisher: $publisher<br><hr><br>";  
  18.     }  
  19.   
  20.     /* 
  21.     脚本首先创建一个 new DOMdocument 对象,用 load 方法把图书 XML 装入这个对象。之后,脚本用 getElementsByName 方法得到指定名称下的所有元素的列表。 
  22.     在 book 节点的循环中,脚本用 getElementsByName 方法获得 author、publisher 和 title 标记的 nodeValue。nodeValue 是节点中的文本。脚本然后显示这些值。 
  23.     */  

 

Php代码   收藏代码
  1. // 用 SAX 解析器读取 XML   
  2.   
  3.     $g_books = array();  
  4.     $g_elem = null;  
  5.         
  6.     function startElement( $parser$name$attrs ){  
  7.         global $g_books$g_elem;  
  8.         if ( $name == 'BOOK' ) $g_books []= array();  
  9.         $g_elem = $name;  
  10.     }  
  11.         
  12.     function endElement( $parser$name ){  
  13.         global $g_elem;  
  14.         $g_elem = null;  
  15.     }  
  16.         
  17.     function textData( $parser$text ){  
  18.         global $g_books$g_elem;  
  19.         if ( $g_elem == 'AUTHOR' || $g_elem == 'PUBLISHER' || $g_elem == 'TITLE' ){  
  20.             $g_bookscount$g_books ) - 1 ][ $g_elem ] = $text;  
  21.         }  
  22.     }  
  23.         
  24.     $parser = xml_parser_create();  
  25.   
  26.     xml_set_element_handler( $parser"startElement""endElement" );  
  27.     xml_set_character_data_handler( $parser"textData" );  
  28.        
  29.     $f = fopen'test.xml''r' );  
  30.         
  31.     while$data = fread$f, 4096 ) ){  
  32.         xml_parse( $parser$data );  
  33.     }  
  34.         
  35.     xml_parser_free( $parser );  
  36.         
  37.     foreach$g_books as $book ){  
  38.         echo $book['TITLE']." - ".$book['AUTHOR']." - ";  
  39.         echo $book['PUBLISHER']."\n";  
  40.     }  
  41.   
  42.     /* 
  43.      
  44.     脚本首先设置 g_books 数组,它在内存中容纳所有图书和图书信息,g_elem 变量保存脚本目前正在处理的标记的名称。然后脚本定义回调函数。在这个示例中,回调函数是 startElement、endElement  和 textData。在打开和关闭标记的时候,分别调用 startElement 和 endElement  函数。在开始和结束标记之间的文本上面,调用 textData。 
  45.      
  46.     在这个示例中,startElement 标记查找 book 标记,在 book  数组中开始一个新元素。然后,textData 函数查看当前元素,看它是不是 publisher、title  或 author 标记。如果是,函数就把当前文本放入当前图书。 
  47.  
  48.     为了让解析继续,脚本用 xml_parser_create 函数创建解析器。然后,设置回调句柄。之后,脚本读取文件并把文件的大块内容发送到解析器。在文件读取之后,xml_parser_free 函数删除解析器。脚本的末尾输出 g_books 数组的内容。 
  49.  
  50.     */  
 
Php代码   收藏代码
  1. // 用正则表达式解析 XML  
  2.   
  3.   $xml = "";  
  4.   $f = fopen'test.xml''r' );  
  5.   while$data = fread$f, 4096 ) ) { $xml .= $data; }  
  6.   fclose( $f );  
  7.     
  8.   preg_match_all( "/\<book\>(.*?)\<\/book\>/s"$xml$bookblocks );  
  9.     
  10.   foreach$bookblocks[1] as $block ){  
  11.       preg_match_all( "/\<author\>(.*?)\<\/author\>/"$block$author );  
  12.       preg_match_all( "/\<title\>(.*?)\<\/title\>/"$block$title );  
  13.       preg_match_all( "/\<publisher\>(.*?)\<\/publisher\>/"$block$publisher );  
  14.       echo$title[1][0]." - ".$author[1][0]." - "$publisher[1][0]."\n" );  
  15.   }  
  16.   
  17.   /* 
  18.   我从不建议使用正则表达式读取 XML,但是有时它是兼容性最好的方式,因为正则表达式函数总是可用的。不要用正则表达式读取直接来自用户的 XML,因为无法控制这类 XML 的格式或结构。应当一直用 DOM 库或 SAX 解析器读取来自用户的 XML。 
  19.   */  
 

编写:

 

Php代码   收藏代码
  1. // 用 DOM 编写 XML  
  2.   
  3.   
  4.   $books = array();  
  5.   $books [] = array(  
  6.       'title' => 'PHP Hacks',  
  7.       'author' => 'Jack Herrington',  
  8.       'publisher' => "O'Reilly"  
  9.   );  
  10.   $books [] = array(  
  11.       'title' => 'Podcasting Hacks',  
  12.       'author' => 'Jack Herrington',  
  13.       'publisher' => "O'Reilly"  
  14.   );  
  15.     
  16.   $doc = new DOMDocument();  
  17.   $doc->formatOutput = true;  
  18.     
  19.   $r = $doc->createElement( "books" );  
  20.   $doc->appendChild( $r );  
  21.     
  22.   foreach$books as $book ){  
  23.       $b = $doc->createElement( "book" );  
  24.         
  25.       $author = $doc->createElement( "author" );  
  26.       $author->appendChild(  $doc->createTextNode( $book['author'] ) );  
  27.     
  28.       $b->appendChild( $author );  
  29.         
  30.       $title = $doc->createElement( "title" );  
  31.       $title->appendChild( $doc->createTextNode( $book['title'] ) );  
  32.     
  33.       $b->appendChild( $title );  
  34.         
  35.       $publisher = $doc->createElement( "publisher" );  
  36.       $publisher->appendChild( $doc->createTextNode( $book['publisher'] ) );  
  37.     
  38.       $b->appendChild( $publisher );  
  39.       $r->appendChild( $b );  
  40.   }  
  41.     
  42.   //echo $doc->saveXML();  
  43.   
  44.     /* 
  45.     在脚本的顶部,用一些示例图书装入了 books 数组。这个数据可以来自用户也可以来自数据库。 
  46.     示例图书装入之后,脚本创建一个 new DOMDocument,并把根节点 books 添加到它。然后脚本为每本书的 author、title 和 publisher 创建节点,并为每个节点添加文本节点。每个 book 节点的最后一步是重新把它添加到根节点 books。 
  47.     使用 DOM 的真正价值在于它创建的 XML 总是格式正确的。但是如果不能用 DOM 创建 XML 时该怎么办? 
  48.     */  
 
Xml代码   收藏代码
  1. <?php  
  2.   
  3.  PHP 编写xml  
  4.   
  5.  $books = array();  
  6.  $books [] = array(  
  7.   'title' => 'PHP Hacks',  
  8.   'author' => 'Jack Herrington',  
  9.   'publisher' => "O'Reilly"  
  10.  );  
  11.  $books [] = array(  
  12.   'title' => 'Podcasting Hacks',  
  13.   'author' => 'Jack Herrington',  
  14.   'publisher' => "O'Reilly"  
  15.  );  
  16. ?>  
  17.   
  18. <books>  
  19. <?php    
  20.  foreach( $books as $book )  
  21.  {  
  22. ?>  
  23.  <book>  
  24.  <title><?php echo( $book['title'] ); ?></title>  
  25.  <author><?php echo( $book['author'] ); ?>  
  26.  </author>  
  27.  <publisher><?php echo( $book['publisher'] ); ?>  
  28.  </publisher>  
  29.  </book>  
  30. <?php  
  31.  }  
  32. ?>  
  33. </books>  
 

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

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf8"?>  
  2.   <books>  
  3.       <book>  
  4.           <author>Jack Herrington</author>  
  5.           <title>PHP Hacks</title>  
  6.           <publisher>O'Reilly</publisher>  
  7.       </book>  
  8.   
  9.       <book>  
  10.           <author>Jack Herrington</author>  
  11.           <title>Podcasting Hacks</title>  
  12.           <publisher>O'Reilly</publisher>  
  13.       </book>  
  14.   </books>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值