【发布时间】:2012-05-16 13:10:39
【问题描述】:
有了这段 XML:
<my_xml>
<entities>
<image url="lalala.com/img.jpg" id="img1" />
<image url="trololo.com/img.jpg" id="img2" />
</entities>
</my_xml>
我必须摆脱图像标签中的所有属性。所以,我做到了:
<?php
$article = <<<XML
<my_xml>
<entities>
<image url="lalala.com/img.jpg" id="img1" />
<image url="trololo.com/img.jpg" id="img2" />
</entities>
</my_xml>
XML;
$doc = new DOMDocument();
$doc->loadXML($article);
$dom_article = $doc->documentElement;
$entities = $dom_article->getElementsByTagName("entities");
foreach($entities->item(0)->childNodes as $child){ // get the image tags
foreach($child->attributes as $att){ // get the attributes
$child->removeAttributeNode($att); //remove the attribute
}
}
?>
不知何故,当我尝试在 foreach 块中删除一个 from 属性时,内部指针似乎丢失了,它并没有删除这两个属性。
还有其他方法吗?
提前致谢。
【问题讨论】:
标签: php domdocument