Home >Backend Development >PHP Tutorial >PHP XPath XML file search and modification implementation code
The code is as follows:
/*
*/
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");
}
?>