从xml中删除节点

时间:2018-06-04 12:33:27

标签: php xml

我目前正在使用Google RSS Feed。以下是我的xml响应。

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>Example</title>
<link>https://www.example.com</link>
<item>
   <g:id>25</g:id>
   <g:title>Item 1</g:title>
   <g:description>Lorem Ipsum</g:description>
</item>
<item>
   <g:id>26</g:id>
   <g:title>Item 2</g:title>
   <g:description>Lorem Ipsum</g:description>
</item>
</channel>
</rss>

现在,我想要删除具有g:id 25的Item。请指导如何从xml代码中删除该节点。提前致谢。尝试使用以下代码,但$ nodelist返回0长度。

    $dom = new DomDocument;
    $id = 25;
    $productId = $id;
    $dom->loadXML($filecontent);
    // Locate the old product node to modify
    $xpath = new DOMXpath($dom);
    $nodelist = $xpath->query("/channel/item/g:id={$productId}");
    $oldnode = $nodelist->item(0);
    // Remove
    $oldnode->removeChild($oldnode);
    echo $dom->saveXML();//Done output XML 

0 个答案:

没有答案
相关问题