将版本添加到生成的XML标记

时间:2012-12-17 10:32:02

标签: php xml rss

我试图制作RSS Feed XML,我发现了一个网站,他们举例说明了XML应该是什么样子:

<?xml version="1.0"?>
<rss version="2.0">
<channel>

<title>The Channel Title Goes Here</title>
<description>The explanation of how the items are related goes here</description>
<link>http://www.directoryoflinksgohere</link>

<item>
<title>The Title Goes Here</title>
<description>The description goes here</description>
<link>http://www.linkgoeshere.com</link>
</item>

<item>
<title>Another Title Goes Here</title>
<description>Another description goes here</description>
<link>http://www.anotherlinkgoeshere.com</link>
</item>

</channel>
</rss>

然而,当我检查我当前的XML时,我注意到我错过了xml和rss标签中的版本。

<xml>
<rss>
<channel>
<title>#####</title>
<description>
#####
</description>
<path>#####</path>

如何将版本添加到XML和RSS的开始标记?

PHP

$newspages = $this->newspages;

$xml = new SimpleXMLElement('<xml/>');

$rss = $xml->addChild('rss');

$channel = $rss->addChild('channel');

$channel->addChild('title', txt('rss.channelname'));
$channel->addChild('description', txt('rss.channeldescription'));
$channel->addChild('path', 'http://'.$_SERVER['HTTP_HOST']);

foreach ($newspages as $newspage) {
    if ($newspage['id'] !== 'news-archive') {
        $item = $channel->addChild('item');
        $item->addChild('title', $newspage['title']);
        $item->addChild('description', $newspage['description']);
        $item->addChild('path', url('###/pageid', array('language'=>$this->language, 'id'=>$newspage['id'])));
    }
}

Header('Content-type: text/xml');

print($xml->asXML());

1 个答案:

答案 0 :(得分:2)

使用addAttribute方法。

<?php

$xml = new SimpleXMLElement('<xml/>');
$xml->addAttribute('version', '1.0');

$rss = $xml->addChild('rss');
$rss->addAttribute('version', '2.0');