且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 XML 树中删除元素,其中属性是 Simple XML PHP 中的特定字符串

更新时间:2023-11-06 20:57:22

SimpleXML 没有移除子节点的功能,
有些情况下你可以做如何删除一个元素在 XML 字符串中?
但取决于 XML 结构

SimpleXML does not have the remove child node feature,
there are cases you are can do How to deleted an element inside XML string?
but is depend on XML structure

DOMDocument 中的解决方案

$doc = new DOMDocument;
$doc->loadXML($noteString['Notes']);

$xpath = new DOMXPath($doc);
$items = $xpath->query( 'note[@url!="http://yahoo.com"]');

for ($i = 0; $i < $items->length; $i++)
{
  $doc->documentElement->removeChild( $items->item($i) );
}