XML source file
<?xml version= "1.0 encoding=" UTF-8 "?>
1) DOMDocument Read XML
<?PHP $doc=NewDOMDocument (); $doc->load ('Person.xml');//reading an XML file$humans = $doc->getelementsbytagname ("Humans");//an array of objects that get humans tagsforeach($humans as$human) {$names= $human->getelementsbytagname ("name");//an array of objects that get the label of name$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 Read XML
<? php $xml _array=simplexml_load_file ('person.xml'// foreach as $tmp) {echo $tmp->name. " - ". $tmp->sex. " - ". $tmp->old. " <br> " ?>
3) Use PHP Regular expressions to memorize the data
4) XmlReader to read XML data
<?PHP $reader=NewXMLReader (); $reader->open ('Person.xml');//reading XML Data$i =1; while($reader->read ()) {//whether to readif($reader->nodetype = = xmlreader::text) {//determine node typeif($i%3) {echo $reader->value;//gets 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 the data in the label, Zhang Ying. But their focus is different, the first three methods of reading the function of XML is designed to read the value in the tag, equivalent to the text () method in jquery, and XmlReader he is not quite the same, His focus is not to read the value in the tag, but to read the label's properties and put the data to be transferred in the attribute (although the method I wrote above still takes the value in the tag, because the XML file is already given, I don't want to get the XML file out).
As an example,
<data name= ' Zhang Ying ' sex= ' male ' old= ' 28′></data>
The XmlReader is designed to read the value of the name sex old in data, and the content is more cumbersome to read. He is the equivalent of jquery in attr (");
The above is purely personal opinion, please correct me. Hope to be of help to everyone.
PHP Methods for manipulating XML