search
Homephp教程php手册通过php添加xml文档内容的方法

通过php添加xml文档内容的方法

Jun 13, 2016 am 09:16 AM
phpxmlcodecontentcreatedocumentmethodAdd toofpass

通过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内容+新添加的内容。
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

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

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor