简体   繁体   中英

Modify XML via PHP and output xml (almost like a filter a feed)

In PHP, I have to filter this rss feed and output new rss feed. Read the feed, see where the dc:creator== badcreatorname and remove the item and put the feed in the same order.

How do I do that? Please help.

<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>LInk.com title</title>
        <link>http://link.com</link>
        <description>Link.com</description>
        <image>
            <url>http://www.link.com/media/feedlogo.gif</url>
            <title>link.com</title>
            <link>http://link.com</link>
        </image>
        <language>en-us</language>
        <copyright>Copyright 2012 </copyright>
        <generator>Generator</generator>

        <item>
            <title><![CDATA[Title goes here]]></title>
            <link><![CDATA[http://link.com/]]></link>
            <guid isPermaLink="true"><![CDATA[http://link.com/]]></guid>
            <description><![CDATA[ Desc]]></description>
            <dc:creator><![CDATA[badcreatorname]]></dc:creator>
            <pubDate>Mon, 17 Sep 2012 15:16:00 EST</pubDate>
            <dc:identifier>898980</dc:identifier>
            <category domain="category"><![CDATA[cat]]></category>
            <category domain="blogger:name"><![CDATA[cat]]></category>
            <enclosure url="pic" length="" type="image/jpeg"/>
        </item>
        <item>
            <title><![CDATA[Title1 goes here]]></title>
            <link><![CDATA[http://link1.com/]]></link>
            <guid isPermaLink="true"><![CDATA[http://link1.com/]]></guid>
            <description><![CDATA[ Desc1]]></description>
            <dc:creator><![CDATA[goodcreatorname]]></dc:creator>
            <pubDate>Mon, 17 Sep 2012 15:16:00 EST</pubDate>
            <dc:identifier>8989801</dc:identifier>
            <category domain="category"><![CDATA[cat]]></category>
            <category domain="blogger:name"><![CDATA[cat]]></category>
            <enclosure url="pic" length="" type="image/jpeg"/>
        </item>
    </channel>
</rss>
<?php
// Load our XML document
$doc = new DOMDocument();
$doc->load('feed.xml');

// Create an XPath object and register our namespaces so we can
// find the nodes that we want    
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('p', 'http://purl.org/dc/elements/1.1/');

// Find the <item> nodes that have a badcreatorname in there
$nodes = $xpath->query("/rss/channel/item[p:creator/text()[ . = 'badcreatorname' ]]");

// Remove the offending nodes from the DOM
for ($i = 0; $i < $nodes->length; $i++) {
    $node = $nodes->item($i);
    $node->parentNode->removeChild($node);
}

// Write our updated XML back to a new file
$doc->save('feedout.xml');

// Or if you want to send the XML content to stdout:
echo $doc->saveXML();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM