students.xml
<?xml version="1.0" encoding="utf-8"?> <students> <student> <name>小明</name> <age>10</age> <introduce>来自广东</introduce> </student> <student> <name>小花</name> <age>21</age> <introduce>来自北京</introduce> </student> <student sex="female"><name>小白</name><age>20</age><introduce>来自上海</introduce></student></students>
stu_del_dom.PHP
<?PHP $xmldoc = new DOMDocument(); //1.创建DOMDocument对象 $xmldoc->load("students.xml"); //2.加载xml文件 //删除一个学生信息 $root = $xmldoc->getElementsByTagName("students")->item(0); //取出根节点 //找到所有student $stus = $xmldoc->getElementsByTagName("student"); $stu = $stus->item(2); //确定要删除的student编号 //$root->removeChild($stu); //第一种删除方法 $stu->parentNode->removeChild($stu); //第二种删除方法 //如果save是源文件名,则会对文件做更新处理 //如果save是新文件名,则会按文件名创建文件并保存 $xmldoc->save("students.xml"); ?>
input:
<?xml version="1.0" encoding="utf-8"?> <students> <student> <name>小明</name> <age>10</age> <introduce>来自广东</introduce> </student> <student> <name>小花</name> <age>21</age> <introduce>来自北京</introduce> </student> </students>