Home > php教程 > php手册 > body text

php XPath对XML文件查找及修改实现代码

WBOY
Release: 2016-06-06 20:38:02
Original
1155 people have browsed it

php XPath对XML文件查找及修改实现代码,需要的朋友可以参考下。

代码如下:
/*



11
content1
2009-10-11


title2
content2
2009-11-11


*/
if(!function_exists('l')) {
function l() {
echo '
***********************************
';
}
}
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");
}
?>
Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template