A. manipulating XML using SimpleXML
There are two traditional ways to work with XML files: SAX and Dom. SAX is based on an event-triggering mechanism that scans an XML file for processing, and the DOM constructs the entire XML file as a DOM tree that is processed through the traversal of the DOM tree. Each of these two methods have advantages and disadvantages, SAX processing ideas are relatively abstract, DOM processing process is relatively cumbersome, are not very suitable for beginners to get started. PHP5 introduced a new set of XML processing functions, called simplexml. Name as a matter of fact, SimpleXML itself small and capable, only a small number of methods to provide functions, but with it to deal with the XML file function is very powerful, the operation is very simple.
1. Creating an XML file
$_xml =<<<XML
"1.0"encoding="Utf-8"?>
1.0
XML parsing test
Zhang San
http:
//
www.ss.com
"
male
"> Zhang San
hostel
http:
//
www.ss.com
"
women /c16>"> who who who who
electric Donkey
http:
//
www.ss.com
"
male > Surname yellow
Xml;$_sxe=NewSimpleXMLElement ($_xml);//creating an Object parsing XML string$_sxe->asxml ('Test.xml');//generating an XML file
2. Loading the XML file
$_sxe= simplexml_load_file ("test.xml"// load XML file / / Output related information // output main information Reflection::export (new// with reflection View details
3. Parsing an XML file
$_sxe= simplexml_load_file ("test.xml"// load XML file / / Output Related information // output main information Reflection::export (new// View details with launch Echo $_sxe->asxml (); // print the entire XML
4. Read the XML data
$_sxe= simplexml_load_file ( " test.xml " ); // reads 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 numeric subscript echo $_sxe->version[ 2 ]; // If you want to print them all, you can use traversal foreach ($_sxe->version as $_version) {echo ' [ ' . $_ Version. ' ] ' ;} // access level Two node name echo $_sxe->user[ 1 ]-> name; // get the name value of all level two nodes foreach ($_sxe->user as $_user) {echo ' [ ' .$_user-> Name. ' ] ' ;} // get the properties of labels for level two nodes echo $_sxe->user[ 1 ]->author->attributes ();
5. Using XPath to get nodes
$_sxe= Simplexml_load_file ("Test.xml");//get node information using XPath$_version = $_sxe->xpath ('/root/version'); Echo $_version[1];//Traverse versionforeach($_version as$_v) {echo'['. $_v.']';}//access Level two nodes$_user = $_sxe->xpath ('/root/user'); Echo $_user[2]->name;//Traverse level Two nodesforeach($_user as$_u) {echo'['. $_u->name.']';}//Accessing PropertiesEcho $_user[1]->author->attributes ();
Two manipulating XML using DOMDocument
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 (Doc object model,dom) provides a tree structure that makes it easy to create and manipulate labels.
1.DOMDocument Parsing xml
// Create a DOMDocument () 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 as $v) {echo $v-nodevalue;}
2.DOMDocument Generating XML
//declaring XML$_doc =NewDOMDocument ('1.0','Utf-8');//typesetting format$_doc->formatoutput =true;//Create a primary label$_root = $_doc->createelement ('Root');//create a one-level label version$_version = $_doc->createelement ('version');//assign a value to the version label$_versiontextnode = $_doc->createtextnode ('1.0');//put values in the version label$_version->appendchild ($_versiontextnode);//put the first-level label version in Root$_root->appendchild ($_version);//write the primary tag to XML$_doc->appendchild ($_root);//Generate XML$_doc->save ('Aaa.xml');
The above describes the PHP creation and parsing XML 1 (36), including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.