且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP从XML文档中删除特定类型的所有元素

更新时间:2022-11-21 22:33:37

目标是删除整个节点而剩下其他节点(实际上还有更多,但为简单起见,此示例显示了所有

Goal is to delete whole node leaving other nodes ( in actual there are more, but for simplicity this example shows all

$dom = new DOMDocument;
$dom->load('places.xml');
foreach ($dom->getElementsByTagName('places') as $places)
{
    $places->parentNode->removeChild($places);
}
echo $dom->saveXml();

将删除文档中任何位置的所有<places>元素,包括所有子元素.

will remove all <places> elements anywhere in the document, including any children.

输出:

<?xml version="1.0"?>
<piletilve_info>

   <other>
      ...
   </other>
</piletilve_info>