How to modify xml content in php

藏色散人
Release: 2023-03-07 21:04:01
Original
2367 people have browsed it

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 "更新成功";
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template