Introduction to the PHP read xml method

  • 2020-05-27 04:29:07
  • OfStack

1. What is xml and what is xml used for

XML(Extensible Markup Language) is the extensible markup language, which, like HTML1, is SGML(Standard Generalized Markup Language). Xml is a cross-platform, content-dependent technology in the Internet environment and is currently a powerful tool for processing structured document information. The extended markup language XML is a simple data storage language that USES 1 series of simple tags to describe data. These tags can be established in a convenient way. Although XML takes up more space than base 2 data, XML is extremely simple and easy to master and use.
XML has many USES. It can be used to store data, to do data exchange, to prompt data for a variety of applications, and so on.
2. php reads the xml method
xml source file
 
<?xml version="1.0 encoding="UTF-8"?> 
<humans> 
<zhangying> 
<name> Zhang Ying </name> 
<sex> male </sex> 
<old>28</old> 
</zhangying> 
<tank> 
<name>tank</name> 
<sex> male </sex> 
<old>28</old> 
</tank> 
</humans> 


1) read DOMDocument xml
 
<?php 
$doc = new DOMDocument(); 
$doc->load('person.xml'); // read xml file  
$humans = $doc->getElementsByTagName( "humans" ); // achieve humans An array of objects for the tag  
foreach( $humans as $human ) 
{ 
$names = $human->getElementsByTagName( "name" ); // achieve name An array of objects of the tag  
$name = $names->item(0)->nodeValue; // achieve node Is the value of, e.g <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) read the xml simplexml
 
<?php 
$xml_array=simplexml_load_file('person.xml'); // will XML The data in the , Read into the array object  
foreach($xml_array as $tmp){ 
echo $tmp->name."-".$tmp->sex."-".$tmp->old."<br>"; 
} 
?> 


3) use php regular expression to remember data
 
<?php 
$xml = ""; 
$f = fopen('person.xml', 'r'); 
while( $data = fread( $f, 4096 ) ) { 
$xml .= $data; 
} 
fclose( $f ); 
//  Read the data from above  
preg_match_all( "/\<humans\>(.*?)\<\/humans\>/s", $xml, $humans ); // Matches the contents of the outermost tag  
foreach( $humans[1] as $k=>$human ) 
{ 
preg_match_all( "/\<name\>(.*?)\<\/name\>/", $human, $name ); // Match the name  
preg_match_all( "/\<sex\>(.*?)\<\/sex\>/", $human, $sex ); // Match gender  
preg_match_all( "/\<old\>(.*?)\<\/old\>/", $human, $old ); // Match age  
} 
foreach($name[1] as $key=>$val){ 
echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."<br>" ; 
} 
?> 


4)xmlreader to read xml data
 
<?php 
$reader = new XMLReader(); 
$reader->open('person.xml'); // read xml data  
$i=1; 
while ($reader->read()) { // Whether or not to read  
if ($reader->nodeType == XMLReader::TEXT) { // judge node type  
if($i%3){ 
echo $reader->value; // achieve node The value of the  
}else{ 
echo $reader->value."<br>" ; 
} 
$i++; 
} 
} 
?> 


3, summary
There are many ways to read xml, just to name a few. 4 methods above can be read out the data of a label, Zhang Ying. Focus is different, but their test before the design of the three methods of reading xml function key, is to read the values in the tag, equivalent to the text jquery () method, and xmlreader? He is not one kind, he is not the focus of reading the values in the label, and read the label attribute, put all the data to be transmitted, attributes (though I wrote above the method or take the values in the label, because xml file has been given, I don't want to work on the xml.
So just to give you an example,
< data name=' reflection 'sex=' male' old='28 ' > < /data >
xmlreader is designed to read the value of name sex old inside data, which is a little bit more cumbersome. It is equivalent to attr in jquery ("); This thing right here.
The above is purely personal, please correct. Hopefully that helped.

Related articles: