如何将RSS数据附加到现有的RSS XML文件中


How can I append RSS data to the existing RSS XML file?

文件夹中存在一个RSS数据XML文件。我必须将新数据附加到RSSXML文件中。我只需要用PHP来做。我有来自web服务的rss数据(条目、标题、url、desc)。我必须附加到现有的rss xml文件,该文件具有以前的数据。

RSS数据格式类似于此

 <rss version="2.0">
  <channel>
    <title>My Site Feed</title>
    <link>http://www.mysitethathasfeed.com/feed/</link>
    <description>
        A nice site that features a feed.
    </description>
    <item>
        <title>Launched!</title>
        <link>http://www.mysitethathasfeed.com/feed/view.php?ID=launched</link>
        <description>
           We just launched the site! Come join the celebration!
        </description>
    </item>
  </channel>
</rss>

我想只使用PHP动态添加另一项。我该怎么做?

看看:文档对象模型

这里有一些东西,可能会让你开始:

$dom = new DOMDocument;
$dom->load('%path-to-your-rss-file%');
$xpath = new DOMXPath($dom);
$channelNode = $xpath->query('/rss/channel')->item(0);
if ($channelNode instanceof DOMNode) {
    // create example item node (this could be in a loop or something)
    $item = $channelNode->appendChild($dom->createElement('item'));
    // the title
    $item->appendChild(
        $dom->createElement('titel', '%title text%')
    );
    
    // the link
    $item->appendChild(
        $dom->createElement('link', '%link text%')
    );
    
    // the description
    $item->appendChild(
        $dom->createElement('description', '%description text%')
    );
}
$dom->save('%path-to-your-rss-file%');

当然,您需要对xml文件进行写访问!

有很多工具可以操作rss提要。

下面是一个使用simplepie的示例。这是给mapgpie的。

更多关于谷歌的信息。