通过php添加xml文档内容的方法
通过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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
