PhpDOM parses xml documents and adds, deletes, modifies, and queries nodes.

Source: Internet
Author: User
PhpDOM is a tree-based parser. you need to regard the xml document as a tree structure (xml document introduction). let's take a look at the following simple xml document: & amp; lt ;? Xmlversion = & quot; 1.0 & quot; encoding = & quot; UT

Php DOM is a tree-based parser, which requires you to regard the xml document as a tree structure (xml document introduction). let's take a look at the following simple xml document:


  
   
   
    
   
    
James
      
   
    
Male
   11   
    
    
   
    
Xiaohong
      
   
    
Female
   21   
    
 

The following operations are performed on the nodes in the class. xml document above:

Php DOM to traverse nodes:


 Load ("class. xml "); $ stus = $ xmldoc-> getElementsByTagName (" stu "); // The value of the access name Node echo $ stus-> item (1) -> getElementsByTagName ("name")-> item (0)-> nodeValue;?>

Add node operation:


 Load ("class. xml "); $ root = $ xmldoc-> getElementsByTagName (" classes ")-> item (0); $ stu_node = $ xmldoc-> createElement (" stu "); // add an attribute $ stu_node-> setAttribute ("intro", "This is a good student") to the student; $ name_node = $ xmldoc-> createElement ("name "); $ name_node-> nodeValue = "www.phpddt.com"; $ stu_node-> appendChild ($ name_node); $ sex_node = $ xmldoc-> createElement ("sex "); $ sex_node-> nodeValue = "male"; $ stu_node-> appendChild ($ sex_node); $ age_node = $ Xmldoc-> createElement ("age"); $ age_node-> nodeValue = 23; $ stu_node-> appendChild ($ age_node); $ root-> appendChild ($ stu_node ); $ xmldoc-> save ("new_class.xml");?>

Delete a node:


 Load ("new_class.xml"); // to delete a file, you must find it $ stus = $ xmldoc-> getElementsByTagName ("stu"); $ stu1 = $ stus-> item (0 ); $ stu1-> parentNode-> removeChild ($ stu1); $ xmldoc-> save ("delete_class.xml");?>

Update node operation:


 Load ("new_class.xml"); // you can update it only when you find it. $ stus = $ xmldoc-> getElementsByTagName ("stu "); // retrieve the first student $ stu1 = $ stus-> item (0); // change the age to 100 $ stutesage = $ stu1-> getElementsByTagName ("age ") -> item (0); $ stutesage-> nodeValue = 100; $ xmldoc-> save ("new_class.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.