Create and parse XML1 (36) in PHP)

Source: Internet
Author: User
: This article mainly introduces how to create and parse XML1 (36) in PHP. if you are interested in the PHP Tutorial, refer to it. 1. use SimpleXML to manipulate XML

There are two traditional approaches to processing XML files: SAX and DOM. Based on the event trigger mechanism, SAX performs a scan on the XML file to complete the processing. DOM constructs the entire XML file into a DOM tree and completes the processing by traversing the DOM tree. These two methods have their own advantages and disadvantages. the processing logic of SAX is relatively abstract, and the DOM processing process is relatively cumbersome, which is not suitable for beginners. PHP5 introduces a new set of XML processing functions, namely SimpleXML. For example, SimpleXML is compact and provides only a few method functions, but it is very powerful and easy to use to process XML files.

1. create an XML file

$ _ Xml = <
 
  
   
    
1.0
   
   
    
Xml parsing test
   
   
    
     
Zhang San
    
    
     
Http://www.ss.com
    "Male"> James
   
   
    
     
Dormitory
    
    
     
Http://www.ss.com
    "Female"> who and who
   
   
    
     
Donkey
    
    
     
Http://www.ss.com
    "Male"> surnamed Huang
   
  Xml; $ _ sxe = new SimpleXMLElement ($ _ xml); // create an object to parse an xml string $ _ sxe-> asXML ('test. XML'); // Generate an xml file
 

2. load XML files

$ _ Sxe = simplexml_load_file ("test. xml "); // load the XML file var_dump ($ _ sxe); // output the relevant information print_r ($ _ sxe); // output the main information Reflection :: export (new ReflectionClass ($ sxe); // use reflection to view details

3. parse XML files

$ _ Sxe = simplexml_load_file ("test. xml "); // load the XML file var_dump ($ _ sxe); // output the relevant information print_r ($ _ sxe); // output the main information Reflection :: export (new ReflectionClass ($ _ sxe); // view the details with the launch echo $ _ sxe-> asXML (); // print the entire XML

4. read XML data

$ _ Sxe = simplexml_load_file ("test. xml "); // read the value of the first-level node, such as the version tag echo $ _ sxe-> version; // if there are multiple, you can set the numeric subscript echo $ _ sxe-> version [2]; // If you want to print all the data, you can traverse foreach ($ _ sxe-> version as $ _ version) {echo '['. $ _ version. ']';} // access the nameecho $ _ sxe of the second-level node-> user [1]-> name; // Obtain the name value foreach ($ _ sxe-> user as $ _ user) {echo '['. $ _ user-> name. ']';} // Obtain the attribute echo $ _ sxe of the label of the second-level node-> user [1]-> author-> attributes ();

5. use XPath to get nodes

$ _ Sxe = simplexml_load_file ("test. xml "); // use XPath to obtain node information $ _ version = $ _ sxe-> xpath ('/root/version'); echo $ _ version [1]; // traverse versionforeach ($ _ version as $ _ v) {echo '['. $ _ v. ']';} // access the second-level node $ _ user = $ _ sxe-> xpath ('/root/user'); echo $ _ user [2]-> name; // traverse the second-level node foreach ($ _ user as $ _ u) {echo '['. $ _ u-> name. ']';} // access attribute echo $ _ user [1]-> author-> attributes ();

2. use DOMDocument to manipulate XML

In many cases, manually generating tags requires that documents be generated from top to bottom. you must ensure that the tags are complete, including the 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 (DOM) provides a tree structure that allows you to easily create and process tags.

1. DOMDocument parsing XML

// Create a DOMDocument () $ _ doc = new DOMDocument (); // load xml $ _ doc-> load ('test. xml '); // Obtain the version label $ _ version =$ _ doc-> getElementsByTagName ('version'); echo $ _ version-> item (2)-> nodeValue; // traverse the version label foreach ($ _ version as $ v) {echo $ v-> nodeValue ;}

2. DOMDocument generates XML

// Declare xml $ _ doc = new DOMDocument ('1. 0', 'utf-8'); // typographical format $ _ doc-> formatOutput = true; // create a primary tag $ _ root =$ _ doc-> createElement ('root '); // create a first-level label version $ _ version =$ _ doc-> createElement ('version '); // assign $ _ versionTextNode =$ _ doc-> createTextNode ('1. 0 '); // add the value to the version label $ _ version-> appendChild ($ _ versionTextNode ); // put the first-level label version in root $ _ root-> appendChild ($ _ version); // write the master label into xml $ _ doc-> appendChild ($ _ root ); // generate xml $ _ doc-> save ('AAA. XML ');

The above describes how to create and parse XML 1 (36) in PHP, including some content. I hope to help my friends who are interested in PHP tutorials.

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.