php的SimpleXML方法读写XML接口文件实例解析_php技巧
May 17, 2016 am 08:42 AM
php
在php5中读写xml文档是非常方便的,可以直接使用php的SimpleXML方法来快速解析与生成xml格式的文件,下面举例说明:
创建一个SimpleXML对象有三种方法:
1.使用new关键字创建
复制代码 代码如下:
$xml="
$rss=new SimpleXMLElement($xml);
2.使用simplexml_load_string()创建
复制代码 代码如下:
$xml="
$rss=simplexml_load_string($xml);
3.使用simplexml_load_file()从一个URL创建
复制代码 代码如下:
$rss=simplexml_load_file("rss.xml");
//或者:
$rss=simplexml_load_file("/rss.xml");//远程文档
具体实例如下:
复制代码 代码如下:
$xml="
$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 of this Website
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 Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD

Hot Article Tags

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 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
