无法从 XML 文档中删除所有子项
<?xml version="1.0" encoding="UTF-8"?>
<routes>
<route name="admin" />
<!---->
<route name="blog" bla bla/>
<route name="blog" bla bla/>
<route name="blog" bla bla/>
</routes>
$xml = simplexml_load_file('routes.xml');
$dom_sxe = dom_import_simplexml($xml); $dom = new \DOMDocument('1.0'); $dom_sxe = $dom->importNode($dom_sxe, true); $dom_sxe = $dom->appendChild($dom_sxe); foreach ($dom->getElementsByTagName('route') as $route) { if($route->getAttribute('name') === 'blog') { $route->parentNode->removeChild($route); echo $route->getAttribute('name'); } } echo $dom->saveXML();
仅删除具有属性blog的 2 个元素