0

I am creating a simple RSS with dynamic content in PHP and I am using following code:

PHP 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';

What result I am getting with above code:

<rss>
    <title>title1</title>
    <link>http://example.com/xyz/?zyx=11008</link>
    <title>title2</title>
    <link>http:/example.com/xyz/?zyx=11009</link>
</rss>

What result I want:

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

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help. I really appreciate your quick response. I just have a quick question, how to add this "<rss version='2.0'>" in my code $root = $doc->createElement('rss');. As in the xml, it is just printing <rss>
You just need to $root = $doc->createElement('rss'); $root->setAttribute("version", "2.0");
btw you can nest the create*() calls into appendChild(), appendChild() will return the appended node. A typical chain is: $parentNode->appendChild($document->createElement('element'))->appendChild($document->createTextNode('text content')). This can avoid a lot of variables, but be careful with the formatting to keep it readable.
btw you can nest the create*() calls into appendChild(), appendChild() will return the appended node. A typical chain is: $parentNode->appendChild($document->createElement('element'))->appendChild($document->createTextNode('text content')). This can avoid a lot of variables, but be careful with the formatting to keep it readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.