使用PHP Simple HTML Dom库时如何从选择中删除元素

时间:2012-01-27 21:42:38

标签: php parsing simplexml

我正在使用PHP Simple HTML Dom库。我可以获得我想要的元素。但是这个元素包含我想从选择中删除的其他元素。

[elem]
    include this data
    [elem]exclude this data[elem]
[elem]

如果可能,请举例说明。

1 个答案:

答案 0 :(得分:0)

XML

<elem>
   include this data
   <elem>exclude this data</elem>
</elem>

php - 纯DOMDocument解决方案:

$dom = new DOMDocument;
$dom->load('xml.xml');
$node = $dom->getElementsByTagName('elem')->item(0);
$child = $node->getElementsByTagName('elem')->item(0);
$node->removeChild($child);
echo $dom->saveXml();

php - 带有DOMDocument的SimpleXML

$doc = simplexml_load_file('xml.xml');

$toremove = $doc->elem;
$dom = dom_import_simplexml($toremove);
$dom->parentNode->removeChild($dom);

echo $doc->asXml();