Object XML parsing function Description
Element xml_set_element_handler() The beginning and end of the element
Character data xml_set_character_data_handler() The beginning of character data
External entity xml_set_external_entity_ref_handler() External entity appears
Unparsed external entity xml_set_unparsed_entity_decl_handler() Unparsed external entity appears
Processing instruction xml_set_processing_instruction_handler() The occurrence of processing instruction
Notation declaration xml_set_notation_decl_handler() The appearance of notation declaration
Default xml_set_default_handler() Other events that do not specify a handling function
Here is a small example of using the parser function to read XML data:
The code of the xml file is as follows:
The results of this program are as follows:
Quote: ----------------------------------------------------------------------------------------------------------------------------
Name: Zhang San Position: Manager
Name: Li Si Position: Assistant
<?xml version="1.0"?>
<employees>
<employee>
<name>Zhang San</name>
<position age="45">Manager</position>
</employee>
<employees>
<employee>
<name>Li Si</name>
<position age="45">Assistant</position>
</employee>
</employees>
<?php
$parser = xml_parser_create(); //Create a parser editor
xml_set_element_handler($parser, "startElement", "endElement");//Set the corresponding functions when the tag is triggered. Here are startElement and endElenment respectively.
xml_set_character_data_handler($parser, "characterData");//Set the corresponding function when reading data
$xml_file="";//Specify the xml file to be read, which can be url
$filehandler = fopen($xml_file, "r");//Open the file
while ($data = fread($filehandler, 4096))
{
xml_parse($parser, $data, feof($filehandler));
}// Take out 4096 bytes each time for processing
fclose($filehandler);
xml_parser_free($parser);//Close and release the parser parser
$name=false;
$position=false;
function startElement($parser_instance, $element_name, $attrs) //Function to start tag event
{
global $name,$position;
if($element_name=="NAME")
{
$name=true;
$position=false;
echo "Name:";
}
if($element_name=="POSITION")
{$name=false;
$position=true;
echo “Position:”;
}
}
function characterData($parser_instance,$xml_data)
{
global $name,$position;
if($position)
echo $xml_data."<br>";
if($name)
echo $xml_data."<br>";
}
function endElement($parser_instance,$element_name) �
{
global $name,$position;
$name=false;
$position=false;
}
?>
Introduction to PHP reading xml method
1. What is xml and what is xml?
XML (Extensible Markup Language) can extend the markup language. Like HTML, it is SGML (Standard Generalized Markup Language, standard general markup language). Xml is a cross-platform, content-dependent technology in the Internet environment, and is a powerful tool for processing structured document information. Extended markup language XML is a simple data storage language that uses a series of simple tags to describe data, which 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, use it to exchange data, prompt data for many application software, etc.
2. How to read xml by php
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) DOMDocument reads xml
<?php
$doc = new DOMDocument();
$doc->load(''); //Read xml file
$humans = $doc->getElementsByTagName( "humans" ); //Get the object array of humans tags
foreach( $humans as $human )
{
$names = $human->getElementsByTagName( "name" ); //Array of object that gets the name tag
$name = $names->item(0)->nodeValue; //Get the value in node, such as <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) Simplexml reads xml
<?php
$xml_array=simplexml_load_file(''); //Read the data in XML 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('', 'r');
while( $data = fread( $f, 4096 ) ) {
$xml .= $data;
}
fclose( $f );
// Read data above
preg_match_all( "/\<humans\>(.*?)\<\/humans\>/s", $xml, $humans ); //Match the contents inside the outermost label
foreach( $humans[1] as $k=>$human )
{
preg_match_all( "/\<name\>(.*?)\<\/name\>/", $human, $name ); //Match the name
preg_match_all( "/\<sex\>(.*?)\<\/sex\>/", $human, $sex ); //Match the 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(''); //Read xml data
$i=1;
while ($reader->read()) { //Whether to read
if ($reader->nodeType == XMLReader::TEXT) { //Judge node type
if($i%3){
echo $reader->value; //Get the value of node
}else{
echo $reader->value."<br>" ;
}
$i++;
}
}
?>
Three, summary
There are many ways to read xml, just to name a few. The above four methods can read out the data in the tag, Zhang Ying. However, their test focus is different. The design focus of the first three methods to read the function of xml is to read the value in the tag, which is equivalent to the text() method in jquery. The xmlreader is different. Its focus is not on reading the values in the tag, but on reading the attributes of the tag and putting the data to be transmitted in the attributes (but the method I wrote above still takes the value in the tag, because the xml file has been given, so I don’t want to make the xml file out).
Let me give you an example to explain,
<data name='Zhang Ying' sex='Male' old='28'></data>
The design focus of xmlreader is to read the value of name sex old in data, and the content read is more troublesome. It is equivalent to the attr("); thing in jquery.
The above is purely personal opinion, please correct me. Hope it will be helpful to everyone.
Element xml_set_element_handler() The beginning and end of the element
Character data xml_set_character_data_handler() The beginning of character data
External entity xml_set_external_entity_ref_handler() External entity appears
Unparsed external entity xml_set_unparsed_entity_decl_handler() Unparsed external entity appears
Processing instruction xml_set_processing_instruction_handler() The occurrence of processing instruction
Notation declaration xml_set_notation_decl_handler() The appearance of notation declaration
Default xml_set_default_handler() Other events that do not specify a handling function
Here is a small example of using the parser function to read XML data:
The code of the xml file is as follows:
The results of this program are as follows:
Quote: ----------------------------------------------------------------------------------------------------------------------------
Name: Zhang San Position: Manager
Name: Li Si Position: Assistant
Copy the codeThe code is as follows:
<?xml version="1.0"?>
<employees>
<employee>
<name>Zhang San</name>
<position age="45">Manager</position>
</employee>
<employees>
<employee>
<name>Li Si</name>
<position age="45">Assistant</position>
</employee>
</employees>
Copy the codeThe code is as follows:
<?php
$parser = xml_parser_create(); //Create a parser editor
xml_set_element_handler($parser, "startElement", "endElement");//Set the corresponding functions when the tag is triggered. Here are startElement and endElenment respectively.
xml_set_character_data_handler($parser, "characterData");//Set the corresponding function when reading data
$xml_file="";//Specify the xml file to be read, which can be url
$filehandler = fopen($xml_file, "r");//Open the file
while ($data = fread($filehandler, 4096))
{
xml_parse($parser, $data, feof($filehandler));
}// Take out 4096 bytes each time for processing
fclose($filehandler);
xml_parser_free($parser);//Close and release the parser parser
$name=false;
$position=false;
function startElement($parser_instance, $element_name, $attrs) //Function to start tag event
{
global $name,$position;
if($element_name=="NAME")
{
$name=true;
$position=false;
echo "Name:";
}
if($element_name=="POSITION")
{$name=false;
$position=true;
echo “Position:”;
}
}
function characterData($parser_instance,$xml_data)
{
global $name,$position;
if($position)
echo $xml_data."<br>";
if($name)
echo $xml_data."<br>";
}
function endElement($parser_instance,$element_name) �
{
global $name,$position;
$name=false;
$position=false;
}
?>
Introduction to PHP reading xml method
1. What is xml and what is xml?
XML (Extensible Markup Language) can extend the markup language. Like HTML, it is SGML (Standard Generalized Markup Language, standard general markup language). Xml is a cross-platform, content-dependent technology in the Internet environment, and is a powerful tool for processing structured document information. Extended markup language XML is a simple data storage language that uses a series of simple tags to describe data, which 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, use it to exchange data, prompt data for many application software, etc.
2. How to read xml by php
xml source file
Copy the codeThe code is as follows:
<?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) DOMDocument reads xml
Copy the codeThe code is as follows:
<?php
$doc = new DOMDocument();
$doc->load(''); //Read xml file
$humans = $doc->getElementsByTagName( "humans" ); //Get the object array of humans tags
foreach( $humans as $human )
{
$names = $human->getElementsByTagName( "name" ); //Array of object that gets the name tag
$name = $names->item(0)->nodeValue; //Get the value in node, such as <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) Simplexml reads xml
Copy the codeThe code is as follows:
<?php
$xml_array=simplexml_load_file(''); //Read the data in XML 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
Copy the codeThe code is as follows:
<?php
$xml = "";
$f = fopen('', 'r');
while( $data = fread( $f, 4096 ) ) {
$xml .= $data;
}
fclose( $f );
// Read data above
preg_match_all( "/\<humans\>(.*?)\<\/humans\>/s", $xml, $humans ); //Match the contents inside the outermost label
foreach( $humans[1] as $k=>$human )
{
preg_match_all( "/\<name\>(.*?)\<\/name\>/", $human, $name ); //Match the name
preg_match_all( "/\<sex\>(.*?)\<\/sex\>/", $human, $sex ); //Match the 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
Copy the codeThe code is as follows:
<?php
$reader = new XMLReader();
$reader->open(''); //Read xml data
$i=1;
while ($reader->read()) { //Whether to read
if ($reader->nodeType == XMLReader::TEXT) { //Judge node type
if($i%3){
echo $reader->value; //Get the value of node
}else{
echo $reader->value."<br>" ;
}
$i++;
}
}
?>
Three, summary
There are many ways to read xml, just to name a few. The above four methods can read out the data in the tag, Zhang Ying. However, their test focus is different. The design focus of the first three methods to read the function of xml is to read the value in the tag, which is equivalent to the text() method in jquery. The xmlreader is different. Its focus is not on reading the values in the tag, but on reading the attributes of the tag and putting the data to be transmitted in the attributes (but the method I wrote above still takes the value in the tag, because the xml file has been given, so I don’t want to make the xml file out).
Let me give you an example to explain,
<data name='Zhang Ying' sex='Male' old='28'></data>
The design focus of xmlreader is to read the value of name sex old in data, and the content read is more troublesome. It is equivalent to the attr("); thing in jquery.
The above is purely personal opinion, please correct me. Hope it will be helpful to everyone.