How to modify the xml file in php: first open the xml file; then traverse the xml document; then reassign the content of the xml file; and finally save the file.
# Recommended: "
PHP Video Tutorial》
PHP Principles and methods of modifying and deleting XML content For example: example.xmltitle1 title2 title3 title4 title5
load('example.xml'); $books = $doc -> getElementsByTagName("book"); //遍历 foreach ($books as $book) { echo $book->getAttribute('id')."-"; echo $book->getElementsByTagName("title")->item(0)->nodeValue; echo "
"; }
2-title2
3-title3
4-title4
5-title5
load('example.xml'); $books = $doc -> getElementsByTagName("book"); //遍历 foreach ($books as $book) { //将id=3的title设置为33333 if($book->getAttribute('id')==3){ echo $book->getAttribute('id')."-"; echo $book->getElementsByTagName("title")->item(0)->nodeValue="33333"; echo "
"; } } //对文件做修改后,一定要记得重新sava一下,才能修改掉原文件 $doc -> save('example.xml');
title1 title2 33333 title4 title5
load('example.xml'); $root = $doc -> documentElement; //根标签 $books = $doc -> getElementsByTagName("book"); //遍历 foreach ($books as $book) { //将id=4的删除 if($book->getAttribute('id')==4){ $root->removeChild($book); } } //对文件做修改后,一定要记得重新sava一下,才能修改掉原文件 $doc -> save('example.xml');
title1 title2 33333 title5
The above is the detailed content of How to modify xml file in php. For more information, please follow other related articles on the PHP Chinese website!