search
Homephp教程php手册php的SimpleXML方法读写XML接口文件实例解析

在php5中读写xml文档是非常方便的,可以直接使用php的SimpleXML方法来快速解析与生成xml格式的文件,下面举例说明:

创建一个SimpleXML对象有三种方法:

1.使用new关键字创建

复制代码 代码如下:


$xml="1aaa16
2bbb26
";
$rss=new SimpleXMLElement($xml);

2.使用simplexml_load_string()创建

复制代码 代码如下:


$xml="1aaa16
2bbb26
";
$rss=simplexml_load_string($xml);

3.使用simplexml_load_file()从一个URL创建

复制代码 代码如下:


$rss=simplexml_load_file("rss.xml");
//或者:
$rss=simplexml_load_file("/rss.xml");//远程文档

具体实例如下:

复制代码 代码如下:


$xml="1aaa162bbb26";
$rss=new SimpleXMLElement($xml);
foreach($rss->item as $v){
 echo $v->name,'
';
}
echo $rss->item[1]->age;//读取数据
echo '


';
$rss->item[1]->name='ccc';//修改数据
foreach($rss->item as $v){
 echo $v->name,'
';//aaa
ccc

}
echo '
';
unset($rss->item[1]);//输出数据
foreach($rss->item as $k=>$v){
 echo $v->name,'
';//aaa

}
echo '
';
//添加数据
$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,'
';//aaa
ccc_new

}
$rss->asXML('personinfo.xml');
?>

进一步分析上面例子如下:

复制代码 代码如下:


//xml数据的读取
//可以直接通过元素的名称来访问特定的元素。文档中的所有元素都被看成是该对象的属性。
foreach($rss->item as $v){
    echo $v->name,'
';//aaa
bbb

}
echo $rss->item[1]->age;//26
//xml数据修改,可以直接利用对象属性赋值的方法来直接编辑一个元素的内容
$rss->item[1]->name='ccc';//修改数据
foreach($rss->item as $v){
    echo $v->name,'
';//aaa
ccc

}
//可以用php内容函数unset来将一个元素从树中删除
unset($rss->item[1]);
foreach($rss->item as $v){
    echo $v->name,'
';//a www.jb51.net aa

}
//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,'
';//aaa
ccc_new

}
//xml数据的存储
//使用对象的asXML()方法
$rss->asXML('personinfo.xml');//将xml数据存储到personinfo.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

Video Face Swap

Video Face Swap

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

Hot Tools

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment