Home  >  Article  >  Backend Development  >  PHP XPath XML file search and modification implementation code

PHP XPath XML file search and modification implementation code

高洛峰
高洛峰Original
2016-11-30 11:29:101138browse

The code is as follows:
/*



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');

//Use the xpath method to find the node location
$query = $xml->xpath('//title[@name="t1"]/ancestor::item');

var_dump($query);
l( );

//Test to add a new child node under the title node new
$query[0]->title->addChild('new','new');

//Test to add a new child node under the title node Add attribute fuck
$query[0]->title->addAttribute('fuck','fuck you');

//Test modify the content node value to hello,world
$query[0]-> content = 'hello,world';

//No explanation, you know
$query[0]->asXML("10.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