XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<pages>
<page><title>Home</title><content>Lorem Ipsum</content></page>
<page><title>Pictures</title><content>Lorem Ipsum</content></page>
<page><title>Information</title><content>Lorem Ipsum</content></page>
</pages>
<css>
<css-tag><title>background-color</title><value>#FFF</value></css-tag>
</css>
<layout>1</layout>
</root>
PHP:
$title = $_GET['0'];
$xml = new DOMDocument('1.0', 'ISO-8859-1');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = true;
$xml->load($location);
$pages = $xml->getElementsByTagName("page");
foreach($pages as $page){
$pagetitle = $page->getElementsByTagName("title");
$pagetitlevalue = $pagetitle->item(0)->nodeValue;
if($title == $pagetitlevalue){
$pagetitle->item(0)->parentNode->removeChild($pagetitle->item(0));
}
}
$xml->save($location);
此代码仅删除 <Title>
节点,如何更改以摆脱父节点 <Page>
节点?
我不知道如何做到这一点,我只是设法摆脱标题节点并获取大量错误代码
最佳答案
虽然我可能会使用 xpath,但你可以找到...
$pagetitle->item(0)->parentNode->removeChild($pagetitle->item(0));
并替换为...
$pagetitle->item(0)->parentNode->parentNode->removeChild($pagetitle->item(0)->parentNode);
在 XML 树中向上一级
关于PHP XML 删除父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10341498/