php5中读写xml文档是非常方便的,我们可以使用php SimpleXML对话来帮我们快速解析与生成xml格式的文件.
创建一个SimpleXML对象有三种方法:
使用new关键字创建,代码如下:
$xml="<personinfo><item><id>1</id><name>aaa</name><age>16</age></item> <item><id>2</id><name>bbb</name><age>26</age></item></personinfo>"; $rss=new SimpleXMLElement($xml); 使用simplexml_load_string()创建,代码如下: $xml="<personinfo><item><id>1</id><name>aaa</name><age>16</age></item> <item><id>2</id><name>bbb</name><age>26</age></item></personinfo>";//开源代码phprm.com $rss=simplexml_load_string($xml); 最常用的方法,使用simplexml_load_file()URL创建,代码如下: $rss=simplexml_load_file("rss.xml");或 $rss=simplexml_load_file("/rss.xml");//远程文档
先看个例子,代码如下:
<?php $xml="<personinfo><item><id>1</id><name>aaa</name><age>16</age></item><item><id>2</id><name>bbb</name><age>26</age></item></personinfo>"; $rss=new SimpleXMLElement($xml); foreach($rss->item as $v){ echo $v->name,'<br />'; } echo $rss->item[1]->age;//读取数据 echo '<hr>'; $rss->item[1]->name='ccc';//修改数据 foreach($rss->item as $v){ echo $v->name,' <br /> ';//aaa <br /> ccc <br /> } echo '<hr>'; unset($rss->item[1]);//输出数据 foreach($rss->item as $k=>$v){ echo $v->name,' <br /> ';//aaa <br /> } echo '<hr>'; //添加数据 $item=$rss->addChild('item'); $item->addChild('id','3'); $item->addChild('name','ccc_new'); $item->addChild('age','40'); foreach($rss->item as $k=>$v){ echo $v->name,' <br /> ';//aaa <br /> ccc_new <br /> } $rss->asXML('personinfo.xml');
xml数据的读取:可以直接通过元素的名称来访问特定的元素,文档中的所有元素都被看成是该对象的属性,实例代码如下:
foreach($rss->item as $v){ echo $v->name,' <br /> ';//aaa <br /> bbb <br /> } echo $rss->item[1]->age;//26
xml数据修改,可以直接利用对象属性赋值的方法来直接编辑一个元素的内容,实例代码如下:
$rss->item[1]->name='ccc';//修改数据 foreach($rss->item as $v){ echo $v->name,' <br /> ';//aaa <br /> ccc <br /> }
可以用php内容函数unset来将一个元素从树中删除,实例代码如下:
unset($rss->item[1]); foreach($rss->item as $v){ echo $v->name,' <br /> ';//a www.phprm.com aa <br /> }
xml添加元素数据,可通过对象的addChild方法来实现,实例代码如下:
$item=$rss->addChild('item'); $item->addChild('id','3'); $item->addChild('name','ccc_new'); $item->addChild('age','40'); foreach($rss->item as $k=>$v){ echo $v->name,' <br /> ';//aaa <br /> ccc_new <br /> }
xml数据的存储:使用对象的asXML()方法
$rss->asXML('personinfo.xml');//将xml数据存储到personinfo.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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
