php操作xml_php实例
要操作的数据
XML几个基本概念
1、 节点:节点也就是很多程序语言中处理XML时的Node,节点是一个比较宽泛的概念,在XML中元素,属性,名字空间,注释,文本内容,处理指令,还有整个文档都属于节点,也就是说XML文档中每个独立的一小部分都是节点,
2、元素:很多程序语言都有对XML处理,节点是一个很宽泛的概念,因为要统一API,对节点不会有过多方法,而元素也就是Element是节点的一个子集,简单讲就是
3、属性:这个比较好理解,在里面的类似XX=”OO”等东西都是属性节点
4、转义字符:和HTML等类似,xml也有语言占用的符号,想使用的这些特殊字符的时候需要转义
DOMDocument对象
我使用的是DOMDocument对象来操作xml,感觉用起来比simpleXml科学一些,当然第一天使用php,纯属个人感觉。DOMDocument有几个常用的属性和方法。
加载xml
$path=$_SERVER["DOCUMENT_ROOT"].'/books.xml';
$books=new DOMDocument();
$books->load($path);
读取/遍历节点与属性
$bookElements=$books->getElementsByTagName('book');
foreach($bookElements as $book){
foreach ($book->attributes as $attr) {
echo strtoupper($attr->nodeName).' —— '.$attr->nodeValue.'
';
}
echo "AUTHOR: ";
foreach ($book->getElementsByTagName('author') as $author) {
echo $author->nodeValue.' ';
}
echo '
';
}

当然对于很多属性,只想读一个,可以通过item(index)方法按索引读取
echo $book->attributes->item(1)->nodeValue;
还可以通过强大的xpath查询
还可以通过强大的xpath查询
修改属性/节点
foreach($bookElements as $book){
foreach ($book->attributes as $attr) {
#$book->setAttribute($attr->nodeName,strtoupper($attr->nodeValue));
$attr->nodeValue=strtoupper($attr->nodeValue);
}
echo "AUTHOR: ";
foreach ($book->getElementsByTagName('author') as $author) {
$author->nodeValue=strtoupper($author->nodeValue);
}
}
$books->save($path);

对属性修改可以直接访问其nodeValue改动,也可以使用setAttribute方法,改动完了别忘了使用save保存。
$book->setAttribute($attr->nodeName,strtoupper($attr->nodeValue));
$attr->nodeValue=strtoupper($attr->nodeValue);
添加元素/属性
$newBook=$books->createElement('book'); #创建新元素
$newBook->setAttribute('name','PHP Objects, Patterns, and Practice');#创建新属性,方法一
$publisher=$books->createAttribute('publisher');#创建新属性,方法二
$publisher->nodeValue='Apress L.P';
$newBook->appendChild($publisher); #把属性添加到元素上
$author=$books->createElement('author');#创建子元素
$author->nodeValue='Matt Zandstra';
$newBook->appendChild($author);#把子元素添加到父元素上
$books->documentElement->appendChild($newBook);#添加整个节点
$books->save($path);
删除属性/节点
$first=$bookElements->item(0);
$first->removeAttribute('publisher');
$second=$bookElements->item(1);
$second->parentNode->removeChild($second);
$books->save($path);

初学php文章肯定有很多谬误,希望大家批评指正,共同进步。

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



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.

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

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

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

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

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
