Ladineko
Ladineko

Reputation: 1981

Adding version to a Generated XML tag

Im trying to make an RSS Feed XML and i spotted a site where they give an example of how the XML should look like :

<?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>

However when i check my current XML i notice i miss the version in the xml and rss tag.

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

How can i add the version to the start tag of XML and 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());

Upvotes: 0

Views: 155

Answers (1)

Husni
Husni

Reputation: 1065

Use addAttribute method.

<?php

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

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

Upvotes: 2

Related Questions