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

PHP XPath XML file search and modification implementation code

高洛峰
Release: 2023-03-01 12:12:01
Original
1174 people have browsed it

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");
}
?>

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template