How to delete xml document content using php

Source: Internet
Author: User
This article mainly introduces how to delete xml documents through php. The example analyzes the two ways to delete XML documents and provides complete annotations, which is very useful, for more information about how to delete xml documents using php, see the following example. Share it with you for your reference. The specific implementation method is as follows:

First case: Delete a student node

The code is as follows:

<? Php
// 1. create a DOMDocument object. This object indicates an xml file.
$ Xmldoc = new DOMDocument ();
// 2. load the xml file (specify the xml file to be parsed, and the dom tree node will be loaded to the memory)
$ Xmldoc-> load ("class. xml ");
// 3. delete a student information record
// (1) extract the root node
$ Root = $ xmldoc-> getElementsByTagName ("class")-> item (0); // This code is not required if it is not deleted by node.
// (2) find the student
$ Stu = $ xmldoc-> getElementsByTagName ("student ");
$ Stu_del = $ stu-> item (2); // find the third student
$ Root-> removeChild ($ stu_del); // perform the delete operation. this is Method 1.
// $ Stu_del-> parentNode-> removeChild ($ stu_del); // locate the parent node and delete it from the parent node. this is Method 2.
// 4. update the xml document
$ Xmldoc-> save ("class. xml ");
Echo "deleted successfully ";
?>


Case 2: delete one of the student nodes, such as age:

The code is as follows:

<? Php
// 1. create a DOMDocument object. This object indicates an xml file.
$ Xmldoc = new DOMDocument ();
// 2. load the xml file (specify the xml file to be parsed, and the dom tree node will be loaded to the memory)
$ Xmldoc-> load ("class. xml ");
// 3. delete a student information record
// (1) extract the root node
$ Root = $ xmldoc-> getElementsByTagName ("class")-> item (0 );
// (2) find the student
$ Stu = $ xmldoc-> getElementsByTagName ("age ");
$ Stu_del = $ stu-> item (1); // locate the age of the second student
$ Stu_del-> parentNode-> removechild ($ stu_del); // perform the delete operation.
// 4. update the xml document
$ Xmldoc-> save ("class. xml ");
Echo "deleted successfully ";
?>

I hope this article will help you with the XML programming for php operations.

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.