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

藏色散人
藏色散人Original
2020-11-26 10:08:332409browse

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.

How to modify xml content in php

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!

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
Previous article:iis hide index.phpNext article:iis hide index.php