PHP XML5 operation: using DOM objects to delete elements in XML documents

Source: Internet
Author: User
In XML entry 8: using DOM objects to add elements to XML documents, we learned how to add elements to XML documents using the php dom class. Of course, dom also provides us with the ability to delete elements from XML documents. in "XML entry 8: using DOM objects to add elements to XML documents, we learned how to add elements to XML documents using the php dom class. Of course, dom also provides the ability to delete elements from XML documents.
Let's take a look at the business logic for deleting elements:
1. load XML documents
2. select the parent element from which you want to delete the element.
3. select the child element to be deleted from the parent element.
4. delete a child element from the parent element

In the previous articles, we have learned the methods required in step 1, step 2, and step 3. now let's learn how to delete elements.
1. removeChild (child );
It is used to delete child elements from the parent element of the system.
The following is a practical example.
Original XML file: Jiang Shen 23 Male 5.9 9: 00 Liu Hao 25 Male 6.9 10: 00 Now we want Element The label php file is as follows: Load (". /3.xml "); // ================================================ ==================================/// output the original XMLecho $ doc-> saveXML (); echo' '; // ================================================ ====================================/// Select the parent element $ element = $ doc-> getElementsByTagName ("people "); $ liuhao = $ element-> item (1 ); // ================================================ ====================================/// select the child element $ element = $ doc-> getElementsByTagName ("sex "); $ liuhao_sex = $ element-> item (1 ); // ================================================ ====================================/// delete a child element $ liuhao-> removeChild ($ liuhao_sex ); // ================================================ ==================================/// output the current XMLecho $ doc-> saveXML (); echo' ';?> The effect on the browser is as follows: Jiang Shen 23 male 5.9 Liu Hao 25 male 6.9 Jiang Shen 23 male 5.9 Liu Hao 25 6.9

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.