【问题标题】:Modify XML via PHP and output xml (almost like a filter a feed)通过 PHP 修改 XML 并输出 xml(几乎就像过滤提要一样)
【发布时间】:2012-09-17 21:01:01
【问题描述】:

在 PHP 中,我必须过滤这个 rss 提要并输出新的 rss 提要。 阅读提要,查看 dc:creator== badcreatorname 的位置并删除该项目并将提要以相同的顺序放置。

我该怎么做?请帮忙。

<?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 xml xml-parsing


    【解决方案1】:
    <?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();
    ?>
    

    【讨论】:

    • 看起来不错,但是PHP short tags?嘘... :)
    • 谢谢...还有一个更改如何在同一个文件上打印结果。例如:我的输入是 feed.xml,这个文件是 feedmodifier.php,它会打印出 feed(不需要 feedout.xml)...再次感谢。
    • 嘿 Jimp,你还能怎么做(因为你说短标签的嘘声)
    • @tv4free:我更新了我的答案以展示如何打印 XML 文档。此外,@jimp 正在评论我的答案的先前版本(我使用 &lt;? 而不是 &lt;?php
    猜你喜欢
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多