PHP Methods for manipulating XML

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.