2

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>节点呢?

我不知道该怎么做,我只是设法摆脱标题节点并获得大量错误代码

4

1 回答 1

2

虽然这是我可能会使用 xpath 的东西,但您可以找到...

$pagetitle->item(0)->parentNode->removeChild($pagetitle->item(0));

并替换为...

$pagetitle->item(0)->parentNode->parentNode->removeChild($pagetitle->item(0)->parentNode);

在您的 XML 树中更上一层楼

于 2012-04-26T22:14:07.937 回答