save("class.xml");" method."/> save("class.xml");" method.">
Home >Backend Development >PHP Problem >How to modify xml content in php
How to modify xml content in php: first create a DOMDocument object; then load the xml file; and finally update the xml document through the "$xmldoc->save("class.xml");" method.
The operating environment of this tutorial: Windows 7 system, PHP version 5.6. This method is suitable for all brands of computers.
Recommended: "PHP Video Tutorial"
How to modify the content of xml document through php:
The specific implementation method is as follows :
The code is as follows:
<?php //1、创建一个DOMDocument对象。该对象就表示 xml文件 $xmldoc = new DOMDocument(); //2、加载xml文件(指定要解析哪个xml文件,此时dom树节点就会加载到内存中) $xmldoc->load("class.xml"); //3、更新一条学生student信息记录,更新她的年龄 //(1)找到该学生 $student = $xmldoc->getElementsByTagName("student"); $stu1 = $student->item(0);//第一个学生 $stu1_age = $stu1->getElementsByTagName("age")->item(0);//查到她的年龄 $stu1_age->nodeValue = 30; //4、更新 xml 文档 $xmldoc->save("class.xml"); echo "更新成功"; ?>
The above is the detailed content of How to modify xml content in php. For more information, please follow other related articles on the PHP Chinese website!