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.