Home  >  Article  >  Backend Development  >  Summary of common methods for reading XML in php_php tips

Summary of common methods for reading XML in php_php tips

墨辰丷
墨辰丷Original
2018-05-23 15:03:081448browse

This article mainly introduces the common methods of reading XML in PHP, and summarizes the relevant operating skills of PHP in reading XML files based on DOMDocument, simplexml, regular and xmlreader in the form of examples. Friends in need can refer to the following

xml source file

<?xml version="1.0 encoding="UTF-8"?>
<humans>
   <zhangying>
     <name>张映</name>
     <sex>男</sex>
     <old>28</old>
   </zhangying>
   <tank>
     <name>tank</name>
     <sex>男</sex>
     <old>28</old>
   </tank>
</humans>

1)DOMDocumentRead xml

<?php
   $doc = new DOMDocument();
   $doc->load(&#39;person.xml&#39;); //读取xml文件
   $humans = $doc->getElementsByTagName( "humans" ); //取得humans标签的对象数组
   foreach( $humans as $human )
   {
     $names = $human->getElementsByTagName( "name" ); //取得name的标签的对象数组
     $name = $names->item(0)->nodeValue; //取得node中的值,如<name> </name>
     $sexs = $human->getElementsByTagName( "sex" );
     $sex = $sexs->item(0)->nodeValue;
     $olds = $human->getElementsByTagName( "old" );
     $old = $olds->item(0)->nodeValue;
     echo "$name - $sex - $old\n";
   }
?>

2)simplexmlRead xml

<?php
   $xml_array=simplexml_load_file(&#39;person.xml&#39;); //将XML中的数据,读取到数组对象中
   foreach($xml_array as $tmp){
     echo $tmp->name."-".$tmp->sex."-".$tmp->old."<br>";
   }
?>

3)Use phpregular expressionTo read data

<?php
   $xml = "";
   $f = fopen(&#39;person.xml&#39;, &#39;r&#39;);
   while( $data = fread( $f, 4096 ) ) {
     $xml .= $data;
   }
   fclose( $f );
   // 上面读取数据
   preg_match_all( "/\<humans\>(.*?)\<\/humans\>/s", $xml, $humans ); //匹配最外层标签里面的内容
   foreach( $humans[1] as $k=>$human )
   {
     preg_match_all( "/\<name\>(.*?)\<\/name\>/", $human, $name ); //匹配出名字
     preg_match_all( "/\<sex\>(.*?)\<\/sex\>/", $human, $sex ); //匹配出性别
     preg_match_all( "/\<old\>(.*?)\<\/old\>/", $human, $old ); //匹配出年龄
   }
   foreach($name[1] as $key=>$val){
     echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."<br>" ;
   }
?>

4)xmlreaderTo read xml data

<?php
   $reader = new XMLReader();
   $reader->open(&#39;person.xml&#39;); //读取xml数据
   $i=1;
   while ($reader->read()) { //是否读取
     if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
       if($i%3) {
         echo $reader->value; //取得node的值
       } else {
         echo $reader->value."<br>" ;
       }
       $i++;
     }
   }
?>

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHPImplementing a complete instance of the Model base class based on mysqli_php tips

PHPDetailed explanation of file upload class examples_php skills

PHPSafe download File method_php tips

The above is the detailed content of Summary of common methods for reading XML in php_php tips. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn