students.xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <students> <student> <name>小明</name> <age>20</age> <introduce>来自广东</introduce> </student> <student> <name>小花</name> <age>21</age> <introduce>来自北京</introduce> </student> </students>
stu_update_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(1); //确定要更新的student编号 $stu_age = $xmldoc->getElementsByTagName("age")->item(0); //定位好更新的Tag $stu_age->nodeValue+=10; //更新age数据 //如果save是源文件名,则会对文件做更新处理 //如果save是新文件名,则会按文件名创建文件并保存 $xmldoc->save("students.xml"); ?>
input:
<?xml version="1.0" encoding="utf-8"?> <students> <student> <name>小明</name> <age>30</age> <introduce>来自广东</introduce> </student> <student> <name>小花</name> <age>21</age> <introduce>来自北京</introduce> </student> </students>