【问题标题】:How can I append RSS data to the existing RSS XML file?如何将 RSS 数据附加到现有的 RSS XML 文件?
【发布时间】:2011-06-16 19:38:24
【问题描述】:

我的文件夹中有一个 RSS 数据 XML 文件。我必须将新数据附加到 RSS XML 文件中。我只需要用 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。我怎样才能做到这一点?

【问题讨论】:

标签: php


【解决方案1】:

看看:Document Object Model

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

$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 文件进行写访问!

【讨论】:

    【解决方案2】:

    lots of tools 用于处理 rss 提要。

    这是一个使用simplepieexampleHere's one 用于 mapgpie。

    Google 上的更多内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 2011-07-16
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多