0

我试图制作一个 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());
4

1 回答 1

2

使用 addAttribute 方法。

<?php

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

$rss = $xml->addChild('rss');
$rss->addAttribute('version', '2.0');
于 2012-12-17T11:21:42.477 回答