2011-06-16 86 views
0

我有一個RSS數據XML文件存在於該文件夾。我必須將新數據附加到RSS XML文件。我只需要用PHP來完成。我有來自web服務的rss數據(item,title,url,desc)。我必須附加到現有的rss xml文件,它有以前的數據。如何將RSS數據附加到現有的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。我怎樣才能做到這一點?

+0

http://php.net/manual/en/book.xml.php – 2011-06-16 11:23:45

+0

[簡單的程序到CRUD節點和xml文件的節點值]的可能重複(http://stackoverflow.com/questions/4906073/A-簡單程序到污物節點和節點值-的-XML的文件) – Gordon 2011-06-16 11:31:19

回答

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文件,!