Reputation: 807
I am creating a simple RSS with dynamic content in PHP and I am using following code:
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('rss');
$root = $doc->appendChild($root);
$xml = simplexml_load_string($xml_data->asXML());
foreach($xml->data->item as $item)
{
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode($item->title);
$text = $title->appendChild($text);
$link = $doc->createElement('link');
$link = $root->appendChild($link);
$text = $doc->createTextNode("http://example.com/xyz/?zyx=".$item->id);
$text = $link->appendChild($text);
}
echo 'Wrote: ' . $doc->save("/directory/jobs00.xml") . ' bytes';
<rss>
<title>title1</title>
<link>http://example.com/xyz/?zyx=11008</link>
<title>title2</title>
<link>http:/example.com/xyz/?zyx=11009</link>
</rss>
<rss>
<channel>
<item>
<title>title1</title>
<link>http://example.com/xyz/?zyx=11008</link>
</item>
<item>
<title>title2</title>
<link>http://example.com/xyz/?zyx=11009</link>
</item>
</channel>
</rss>
So what i need to modify in my code in order to achieve what i want above.
Upvotes: 0
Views: 473
Reputation: 57131
You may have to adjust the way this is built up, but it is simply an extension of what you currently have...
$xml = simplexml_load_string($xml_data->asXML());
$channel = $doc->createElement('channel');
$root->appendChild($channel);
foreach($xml->data->item as $item)
{
$title = $doc->createElement('title');
$text = $doc->createTextNode($item->title);
$text = $title->appendChild($text);
$link = $doc->createElement('link');
$text = $doc->createTextNode("http://example.com/xyz/?zyx=".$item->id);
$text = $link->appendChild($text);
$item = $doc->createElement('item');
$item->appendChild($title);
$item->appendChild($link);
$channel->appendChild($item);
}
Upvotes: 1