Home > Backend Development > PHP Tutorial > Introduction to PHP reading xml method_PHP tutorial

Introduction to PHP reading xml method_PHP tutorial

WBOY
Release: 2016-07-21 15:14:14
Original
820 people have browsed it

1. What is xml and what is it used for?

XML (Extensible Markup Language) is an extensible markup language. Like HTML, it is SGML (Standard Generalized Markup Language). 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. Extensible Markup Language XML is a simple data storage language that uses a series of simple tags to describe data, and these tags can be established in a convenient way. Although XML takes up more space than binary data, XML is extremely Simple and easy to master and use.
XML has many uses. It can be used to store data, exchange data, prompt data for many kinds of application software, etc.
Second, how to read xml with php
🎜>

Zhang YingMale 28
/name>
Male
28



 1) DOMDocument reads xml



Copy code

The code is as follows:

$doc = new DOMDocument();
$doc->load('person.xml'); //Read xml file
$humans = $doc->getElementsByTagName( "humans" ); //Get the object array of the humans tag
foreach( $humans as $human ) { $names = $human->getElementsByTagName( "name" ); //Get the object array of the tag of name $name = $names->item(0)->nodeValue; //Get the value in node, such as $sexs = $human->getElementsByTagName ( "sex" );
$sex = $sexs->item(0)->nodeValue;
$olds = $human->getElementsByTagName( "old" );
$old = $olds->item(0)->nodeValue;
echo "$name - $sex - $oldn";
}
?>



 2) simplexml reads xml



Copy code

The code is as follows:

$xml_array=simplexml_load_file ('person.xml'); //Read the data in XML into the array object
foreach($xml_array as $tmp){
echo $tmp->name."-".$ tmp->sex."-".$tmp->old."
";
} ?>
3) Use php Regular expression to remember data



Copy code

The code is as follows:

$xml = "" ;
$f = fopen('person.xml', 'r');
while( $data = fread( $f, 4096 ) ) {
$xml .= $data; } fclose( $f ); // Read data abovepreg_match_all( "/(.*?)/s", $xml, $humans ) ; //Match the content in the outermost tag
foreach( $humans[1] as $k=>$human )
{
preg_match_all( "/(.*?) /", $human, $name ); //Match the name
preg_match_all( "/(.*?)/", $human, $sex ) ; //Match gender
preg_match_all( "/(.*?)/", $human, $old ); //Match age
}
foreach ($name[1] as $key=>$val){
echo $val." - ".$sex[$key][1]." - ".$old[$key][1] ."
" ;
}
?>



 4) xmlreader to read xml data



Copy the code

The code is as follows:

$reader = new XMLReader();
$reader->open('person.xml' ); //Read xml data
$i=1;
while ($reader->read()) { //Whether to readif ($reader->nodeType == XMLReader ::TEXT) { //Judge the node typeif($i%3){ echo $reader->value; //Get the value of the node}else{
echo $reader ->value."
" ;
}
$i++;
}
}
?>



3. Summary
There are many ways to read xml, here are a few. The above four methods can all read the data in the tag, Zhang Ying. But their testing focus is different. The design focus of the function of reading xml in the first three methods is to read the value in the tag, which is equivalent to The text() method in jquery is different from xmlreader. Its focus is not on reading the value in the tag, but on reading the attributes of the tag and putting all the data to be transmitted in the attributes (but as I mentioned above The method I wrote still takes the value in the tag, because the xml file has been given, and I don’t want to create the xml file again).
Give an example to explain,
php xml one What introduce method yes have use read
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template