PHP Create and Parse XML

Source: Internet
Author: User
Tags new set xpath

A   Use  SimpleXML  manipulate  xml to process xml  files, there are two traditional ways of dealing with:sax  and DOM. sax  based on the event-triggering mechanism, the xml  file is scanned once, and the processing is done;dom  the entire xml  file is constructed as a DOM tree, which is processed through the traversal of the dom  tree. Each of the two methods have advantages and disadvantages,sax  processing ideas relative abstract,dom  process relatively cumbersome, are not suitable for beginners to get started. php5  introduced a new set of xml  processing functions, namely simplexml. Name as in fact,simplexml  itself small and capable, only to provide a small number of methods functions, but with it to deal with the xml  file function is very powerful, the operation is very simple. 1. Create an XML file $_xml =<<<xml<?xml version= "1.0"  encoding= "Utf-8"?><root>< Version>1.0</version><info>xml Analytical Testing </info><user><name> Club </name><url >http://www.yc.com</url><author sex= "Male" > Li </author></user><user><name > North Wind Net </name><url>http://www.ibeifeng.com</url><author sex= "female" > Who who </author ></user><user><name> Electric Donkey </name><url>http://www.verycd.com</url><author  sex= "Male" > </author></user></root>xml;$_sxe= new simplexmlele of the surname Yellowment ($_xml);  //Create object Parsing XML string $_sxe->asxml (' Test.xml ');  //generate XML File 2. Load XML file $_sxe= simplexml_load _file ("Test.xml");  //load XML file Var_dump ($_sxe);  //output related information print_r ($_SXE);  //Output main information reflection::export (New reflectionclass ($SXE));  //with reflection View details 3. Parse the XML file $_sxe= simplexml_load_file ("Test.xml");  // Load XML file Var_dump ($_sxe),  //output related information print_r ($_SXE);  //output main information reflection::export (New reflectionclass ( $_SXE)  //Use the launch to view details echo $_sxe->asxml ();//print the entire XML4. read out the XML data $_sxe= simplexml_load_file ("Test.xml ");//Read the value of the first-level node, such as the version label echo $_sxe->version;//if there are more than one, you can set its digital subscript echo $_sxe->version[2];// If you want to print it all out, you can use Traverse foreach  ($_sxe->version as $_version)  {echo  ' ['. $_version. ';} Access to level two node nameecho $_sxe->user[1]->name;//gets the name value of all two-level nodes foreach  ($_sxe->user as $_ User)  {echo  ' ['. $_user->name. '] ';} Gets the attribute of the label for level Two node echo $_sxe->user[1]->author->attributes (); 5. Use XPath to get the sectionPoint $_sxe= simplexml_load_file ("Test.xml");//Use XPath to get node information $_version = $_sxe->xpath ('/root/ Version '); echo $_version[1];//traverse versionforeach  ($_version as $_v)  {echo  ' ['. $_v. ‘]‘;} Access level two node $_user = $_sxe->xpath ('/root/user '); echo $_user[2]->name;//traverse Level Two node foreach  ($_ User as $_u)  {echo  ' [$_u->name. '] ';} Access attribute echo $_user[1]->author->attributes (); second.   Use  DOMDocument  manipulate  xml in many cases, manually generated markup requires a document to be generated from top to bottom, and you must ensure that the labels are complete, start and end tags. Although some PHP functions or classes can be improved, PHP also provides a set of more helpful built-in objects and functions. The Document Object Model (DOCUMENT&NBSP;OBJECT&NBSP;MODEL,DOM) provides a tree-like structure that makes it easy to create and manipulate labels. 1.DOMDocument Parse xml//Create a DOMDocument () $_doc = new domdocument ();//Load Xml$_doc->load (' Test.xml ' );//Take version label $_version = $_doc->getelementsbytagname (' version '); Echo $_version->item (2)- >nodevalue;//traverse the version label foreach  ($_version as  $v)  {echo  $v->nodevalue;} 2.DOMDocument Generating xml//Declaration xml$_doc = new domdocument (' 1.0 ', ' utf-8 ');//typesetting format $_doc->formatoutput = true;//creating a primary label $_root  = $_doc->createelement (' root ');//Create a one-level label version$_version = $_doc->createelement (' version $_versiontextnode = $_doc->createtextnode (' 1.0 ') is assigned to the version tag, or the value is placed in the version label $_version-> AppendChild ($_versiontextnode);//Add the first label version to root $_root->appendchild ($_version);//write the primary tag to xml$_doc-> AppendChild ($_root);//Generate Xml$_doc->save (' aaa.xml ');

PHP Create and parse XML

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.