通过php添加xml文档内容的方法
代码如下:
//1、创建一个DOMDocument对象。该对象就表示 xml文件
$xmldoc = new DOMDocument();
//2、加载xml文件(指定要解析哪个xml文件,此时dom树节点就会加载到内存中)
$xmldoc->load("class.xml");
//3、添加一条学生信息
//(1)取出要节点
$root = $xmldoc->getElementsByTagName("class")->item(0);//返回DOMElement对象类型
var_dump($root);
//(2)创建学生节点 student
$stu_node = $xmldoc->createElement("student");//返回DOMElement对象类型
$stu_node->setAttribute("id","大美女");//给创建的节点添加属性,如果有需要的话
//(3)创建名字、性别、年龄等节点 name、sex和age
$stu_node_name = $xmldoc->createElement("name");
$stu_node_name->nodeValue = "大乔";
$stu_node_sex = $xmldoc->createElement("sex");
$stu_node_sex->nodeValue = "女";
$stu_node_age = $xmldoc->createElement("age");
$stu_node_age->nodeValue = "25";
//(4)将name、sex、age等三个节点挂载到student节点上
$stu_node->appendchild($stu_node_name);
$stu_node->appendchild($stu_node_sex);
$stu_node->appendchild($stu_node_age);
//(5)把student节点挂载到根节点上
$root->appendchild($stu_node);
//4、保存到 xml 文档中
//$xmldoc->save("class.xml");//保存到原xml文档中,相当于在后面添加;如果是一个不存在的xml文档,则会新建一个xml文档,内容为原xml内容+新添加的内容。
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor