Home  >  Article  >  Backend Development  >  How to delete the content of xml document through php_PHP tutorial

How to delete the content of xml document through php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:05921browse

How to delete the content of xml document through php

This article describes the method of deleting the content of xml document through php. Share it with everyone for your reference. The specific implementation method is as follows:

The first situation: delete a student node

The code is as follows:

//1. Create a DOMDocument object. This object represents the xml file
$xmldoc = new DOMDocument();
//2. Load the xml file (specify which xml file to parse, then the dom tree node will be loaded into the memory)
$xmldoc->load("class.xml");
//3. Delete a student information record
//(1) Take out the root node
$root=$xmldoc->getElementsByTagName("class")->item(0);//When not deleting by node, this code is not needed
//(2)Find the student
$stu = $xmldoc->getElementsByTagName("student");
$stu_del = $stu->item(2);//Find the third student
$root->removeChild($stu_del);//Perform deletion operation, this is method 1
//$stu_del->parentNode->removeChild($stu_del);//Find its parent node, and the parent node performs the deletion operation. This is method 2
//4. Update xml document
$xmldoc->save("class.xml");
echo "Delete successfully";
?>


Second case: Delete one of the nodes under the student node, such as age:

The code is as follows:

//1. Create a DOMDocument object. This object represents the xml file
$xmldoc = new DOMDocument();
//2. Load the xml file (specify which xml file to parse, then the dom tree node will be loaded into the memory)
$xmldoc->load("class.xml");
//3. Delete a student information record
//(1) Take out the root node
$root=$xmldoc->getElementsByTagName("class")->item(0);
//(2)Find the student
$stu = $xmldoc->getElementsByTagName("age");
$stu_del = $stu->item(1);//Find the age of the second student
$stu_del->parentNode->removechild($stu_del);//Perform deletion operation
//4. Update xml document
$xmldoc->save("class.xml");
echo "Delete successfully";
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/947793.htmlTechArticleHow to delete the content of xml document through php. This article describes the method of deleting the content of xml document through php. Share it with everyone for your reference. The specific implementation method is as follows: The first situation:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn