DOM Remove Child from RSS XML

北城以北 提交于 2019-12-04 07:09:57

问题


Hoping I can get some help with this, PHP is not my strong point, still learning the ropes...

So I have a php form that creates an RSS file, the RSS feed will be used on an internal intranet by helpdesk staff to keep up to date with current issues that arise in the network.

The form is intended to be used as a front end for managing the feed and the last thing I need to do is be able to delete a specific item from the feed.

I want to delete item's with specific GUID's, when the form creates the item the GUID is simply given a value using time(), I'm stuck at how I even find the item I want to delete, I guess I need to search the items to match the guid element then delete the child it appears in... can someone give me some helpful pointers?

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <item>
      <title>Exchange Server being rebooted</title>
      <description>Exchange servers are currently being rebooted</description>
      <author>Self</author>
      <link></link>
      <pubDate>Fri, 26 Aug 2011 08:37:12</pubDate>
      <guid>1314311832</guid>
    </item>
  </channel>
</rss>

When I display the items from the RSS in the form I'll be putting a link with the GUID that when clicked should delete that item.

Hope this makes sense.

Thanks,


回答1:


You may use DOMXPath to retrieve the GUID-elements and removeChild() to remove them:

$xpath    = new DOMXPath($yourDOMDocument);
$query = '//rss/channel/item/guid[boolean(.="1314311832")]';
$entries  = $xpath->query($query);

foreach ($entries as $entry) {
    $entry->parentNode->parentNode->removeChild($entry->parentNode);
}


来源:https://stackoverflow.com/questions/7198245/dom-remove-child-from-rss-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!