Leena Lavanya
Leena Lavanya

Reputation: 333

Create an RSS Element with Namespace using SimpleXML in PHP

I'm trying to create a rss feed and one element is

<content:encoded></content:encoded>

But, when i use this code:

$item->addChild('content:encoded',htmlspecialchars($itemdata->description));

I get this as a result:

<encoded> .................. </encoded>

I don't get the content namespace, and how would I be able to?

Upvotes: 2

Views: 580

Answers (1)

har07
har07

Reputation: 89285

As you can see in the documentation, you need to provide the namespace URI as 3rd argument of addChild() to create element in namespace correctly :

$item->addChild(
        'content:encoded',
        htmlspecialchars($itemdata->description),
        'namespace-URI-for-content-prefix-here'
    );

Quick demo :

$raw = '<root xmlns:content="mynamespace"></root>';
$item = new SimpleXMLElement($raw);
$item->addChild(
          'content:encoded',
          'foo bar baz',
          'mynamespace'
      );
echo $item->asXML();

eval.in demo

output :

<?xml version="1.0"?>
<root xmlns:content="mynamespace"><content:encoded>foo bar baz</content:encoded></root>

Upvotes: 2

Related Questions