首页 > 开发 > PHP > 正文

php读取xml文件的三种实现方法

2024-05-04 21:58:38
字体:大 中 小
来源:转载
供稿:网友

文章介绍了三种方式来读取xml文件分别是new DOMDocument(),正则解析xml,用parser函数来读取xml数据,这些方法都是可行的,但第一种和最后一种要好一些.

new DOMDocument()实例代码如下:

  1. <?php 
  2.   $doc = new DOMDocument(); 
  3.   $doc->load( 'books.xml' ); 
  4.    
  5.   $books = $doc->getElementsByTagName( "book" ); 
  6.   foreach( $books as $book ) 
  7.   { 
  8.   $authors = $book->getElementsByTagName( "author" ); 
  9.   $author = $authors->item(0)->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 - $author - $publisher/n"; 
  18.   } 
  19.   ?> 

正则解析,代码如下:

  1. <?php 
  2.   $xml = ""; 
  3.   $f = fopen( 'books.xml', 'r' ); 
  4.   while( $data = fread( $f, 4096 ) ) { $xml .= $data; } 
  5.   fclose( $f ); 
  6.    
  7.   preg_match_all( "//<book/>(.*?)/<//book/>/s",  
  8.   $xml, $bookblocks ); 
  9.    
  10.   foreach( $bookblocks[1] as $block ) 
  11.   { 
  12.   preg_match_all( "//<author/>(.*?)/<//author/>/",  
  13.   $block, $author ); 
  14.   preg_match_all( "//<title/>(.*?)/<//title/>/",  
  15.   $block, $title ); 
  16.   preg_match_all( "//<publisher/>(.*?)/<//publisher/>/",  
  17.   $block, $publisher ); 
  18.   echo( $title[1][0]." - ".$author[1][0]." - ". 
  19.   $publisher[1][0]."/n" ); 
  20.   } 
  21.   ?> 

books.xml文件如下:

  1. <books> 
  2.   <book> 
  3.   <author>Jack Herrington</author> 
  4.   <title>PHP Hacks</title> 
  5.   <publisher>O'Reilly</publisher> 
  6.   </book> 
  7.   <book> 
  8.   <author>Jack Herrington</author> 
  9.   <title>Podcasting Hacks</title> 
  10.   <publisher>O'Reilly</publisher> 
  11.   </book> 
  12.   </books>  

下面就给大家举一个小小的例子用parser函数来读取xml数据,代码如下:

  1. <?php 
  2. $parser = xml_parser_create(); //创建一个parser编辑器 
  3. xml_set_element_handler($parser, "startElement", "endElement");//设立标签触发时的相应函数 这里分别为startElement和endElenment 
  4. xml_set_character_data_handler($parser, "characterData");//设立数据读取时的相应函数 
  5. $xml_file="1.xml";//指定所要读取的xml文件,可以是url 
  6. $filehandler = fopen($xml_file, "r");//打开文件 
  7. while ($data = fread($filehandler, 4096))  
  8. { 
  9.     xml_parse($parser, $data, feof($filehandler)); 
  10. }//每次取出4096个字节进行处理 
  11.  
  12. fclose($filehandler); 
  13. xml_parser_free($parser);//关闭和释放parser解析器 
  14.  
  15.  
  16. $name=false; 
  17. $position=false; 
  18. function startElement($parser_instance, $element_name, $attrs)        //起始标签事件的函数 
  19.  { 
  20.    global $name,$position;   
  21.    if($element_name=="NAME") 
  22.    { 
  23.    $name=true; 
  24.    $position=false; 
  25.    echo "名字:"; 
  26.   } 
  27.   if($element_name=="POSITION") 
  28.    {$name=false; 
  29.    $position=true; 
  30.    echo "职位:"; 
  31.   } 
  32. } 
  33.  
  34. function characterData($parser_instance, $xml_data)                  //读取数据时的函数  
  35. { 
  36.    global $name,$position; 
  37.    if($position) 
  38.     echo $xml_data."<br>"; 
  39.     if($name) 
  40.      echo $xml_data."<br>"; 
  41. } 
  42. //开源代码Vevb.com 
  43. function endElement($parser_instance, $element_name)                 //结束标签事件的函数 
  44. { 
  45.  global $name,$position;  
  46. $name=false; 
  47. $position=false;   
  48. } 
  49.  
  50. ?> 

xml文件代码如下:

  1. <?xml version="1.0"?> 
  2. <employees> 
  3. <employee> 
  4. <name>张三</name> 
  5. <position age="45">经理</position> 
  6. </employee> 
  7. <employees> 
  8. <employee> 
  9. <name>李四</name> 
  10. <position age="45">助理</position> 
  11. </employee> 
  12. </employees> 

parser是php内置的一个用来处理xml的解析器,它的工作由三个事件组成:起始标签、 读取数据、结束标签.

也就是说在对xml进行处理的时候每当遇到起始标签、数据和结束标签的时候函数会做相应的动作来完成对xml数据的转换.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表