create an xml tag with colon using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madhu
Forum Commoner
Posts: 82
Joined: Fri Mar 03, 2006 9:34 am

create an xml tag with colon using php

Post by madhu »

HI all,

Please help me how to create an xml tag with colon.


CODE:

<?php
$doc=new DomDocument('1.0','iso-8859-1');
$rss=$doc->createElement('rss');
$rss=$doc->appendChild($rss);
$mediacontent=$doc->createElement('media:content');
$mediacontent=$rss->appendChild($mediacontent);
?>


ERROR:

Its giving reference to undeclared namespace prefix:'media'.

Please help me how to solve this issue.

Waiting for your positive replies.........

Thanks and regards
MADHU
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://www.feedforall.com/mediarss.htm
Declaration: The name space for Media RSS is defined at http://search.yahoo.com/mrss

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss">
Take a look at http://de2.php.net/manual/en/function.d ... espace.php
madhu
Forum Commoner
Posts: 82
Joined: Fri Mar 03, 2006 9:34 am

Post by madhu »

am able to generate

<rss>
<media url="http://d.yimg.com/us.yimg.com/p/ap/2006 ... 3C9pMxoA--" type="image/jpeg" height="91" width="130" />
</rss>

but when i was trying like,

<rss>
<media:content url="http://d.yimg.com/us.yimg.com/p/ap/2006 ... 3C9pMxoA--" type="image/jpeg" height="91" width="130" />
</rss>


its giving the error
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

yes, because the namespace media is not defined.
namespace:name
media:content
madhu
Forum Commoner
Posts: 82
Joined: Fri Mar 03, 2006 9:34 am

Post by madhu »

Dear sir,

Can you please tell me how to define namespace??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

madhu wrote:Can you please tell me how to define namespace??
No, sorry, I can't. Since switching to php 5 I haven't used domxml.
But
madhu
Forum Commoner
Posts: 82
Joined: Fri Mar 03, 2006 9:34 am

Post by madhu »

Hi all,

If anybody knows how to print an XML tag with (:) symbol in the tag,then please help me.

Am searching from the last 2 days in Google.

But am not getting.

Please reply ASAP...........

Thanks and regards
MADHU
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I can tell you how to do it with php5's DOM.
You have to declare the namespace first, i.e. add the attribute xmlns:media with the url to the definition (xmlns, xml namespace).
The namespace for Media RSS is defined to be: http://search.yahoo.com/mrss/

For example:

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
http://de2.php.net/manual/en/function.d ... espace.php probably would have done this for you.

Code: Select all

<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
$rss = $dom->createElement('rss');
$rss->setAttribute('version', '2.0');
// declaring the namespace media
$rss->setAttribute('xmlns:media', 'http://search.yahoo.com/mrss/');

// creating an element in the namespace media
$content = $dom->createElement('media:content');
$content->setAttribute('url', 'http://www.php.net');

$rss->appendChild($content);
$dom->appendChild($rss);

$dom->formatOutput=true;
echo $dom->saveXML();
?>
Post Reply