<?php
/*
<?xml version="1.0" encoding="utf-8"?>
<article>
<item>
<title>11</title>
<content>content1</content>
<pubdate>2009-10-11</pubdate>
</item>
<item>
<title>title2</title>
<content>content2</content>
<pubdate>2009-11-11</pubdate>
</item>
</article>
*/
if(!function_exists('l')) {
function l() {
echo '<br/>***********************************<br />';
}
}
if(file_exists('test10_12.xml')) {
$xml = simplexml_load_file('test10_12.xml');
//利用xpath方法查找节点位置
$query = $xml->xpath('//title[@name="t1"]/ancestor::item');
var_dump($query);
l();
//测试在title节点下新增子节点new
$query[0]->title->addChild('new','new');
//测试在title节点下新增属性fuck
$query[0]->title->addAttribute('fuck','fuck you');
//测试修改content节点值为hello,world
$query[0]->content = 'hello,world';
//不解释,你懂的
$query[0]->asXML("10.xml");
}
?>
php XPath对XML文件查找及修改实现代码
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/100d3307750ba4fbac0d977a6691a089.html