Home >Backend Development >PHP Tutorial >Introduction to xml reading related functions in php 1_PHP tutorial

Introduction to xml reading related functions in php 1_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:52:22936browse

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() The external entity appears
Unparsed external entity xml_set_unparsed_entity_decl_ handler() The occurrence of unresolved external entities
The occurrence of processing instructions xml_set_processing_instruction_handler() The occurrence of processing instructions
The occurrence of notation declaration xml_set_notation_decl_handler() The occurrence of notation declaration
Default xml_set_default_handler() Other events that do not specify a handler function

The following is a small example of using the parser function to read xml data:

The xml file code is as follows:

The results of this program are as follows:

Quote:------------------------------------------------ --------------------------------
Name: Zhang San Position: Manager
Name: Li Si Position : Assistant

Copy code The code is as follows:


< ;employees>

Zhang San
Manager


李思
Assistant

🎜>
$parser = xml_parser_create(); //Create a parser editor
xml_set_element_handler($parser, "startElement", "endElement"); //Set up when the tag is triggered The corresponding functions are startElement and endElenment respectively
xml_set_character_data_handler($parser, "characterData");//Set up the corresponding function when reading data
$xml_file="1.xml";//Specify the content to be read xml file, 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 for processing each time

fclose($filehandler);
xml_parser_free($ parser);//Close and release the parser parser

$name=false;
$position=false;
function startElement($parser_instance, $element_name, $attrs) //Start tag Event function
{
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) //Function when reading data
{
global $name,$position;
if($position)
echo $xml_data."
";
if($name)
echo $xml_data."
";
}

function endElement($parser_instance, $element_name) //Function to end tag event
{
global $name,$position;
$name=false;
$position=false;
}

?>



Introduction to PHP reading xml method

1. What is xml and what is the use of xml
XML (Extensible Markup Language) is an extensible markup language. Like HTML, it is SGML (Standard Generalized Markup Language, 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. 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
XML source file
Copy code The code is as follows:




Zhang YingMale
28

/name>
Men
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 Object array of humans tags
foreach( $humans as $human )
{
$names = $human->getElementsByTagName( "name" ); //Get object array of tags for 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 reading 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 expressions 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 the data above
preg_match_all( "/(.*?)/s", $xml, $humans ); //Match inside the outermost tag Contents of
foreach( $humans[1] as $k=>$human )
{
preg_match_all( "/(.*?)/", $ human, $name ); //Match 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 code The code is as follows:
$reader = new XMLReader();
$reader->open('person.xml'); //Read xml data
$ i=1;
while ($reader->read()) { //Whether to read
if ($reader->nodeType == XMLReader::TEXT) { //Determine the node type
if($i%3){
echo $reader->value; //Get the value of 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,