$xml = new SimpleXMLElement('');
unset($xml->b);
echo $xml->asxml();
由于调用了__unset()方法(或模块代码中的等效方法),因此可以按预期工作(从文档中删除元素)。
但是,当您调用unset($parent);它时,它只会删除存储在$ parent中的对象引用,但不会影响对象本身或$ xml中存储的文档。为此,我将恢复为DOMDocument。
$doc = new DOMDOcument;
$doc->loadxml('
123
456
789
');
$item_id = 456;
$xpath = new DOMXpath($doc);
foreach($xpath->query('//items[info/item_id="' . $item_id . '"]') as $node) {
$node->parentNode->removeChild($node);
}
echo $doc->savexml();
版画
123
789