tags:

views:

96

answers:

2

How would i retrieve what is inside these XML tags in php?

I can get it to work when they don't have a ':' inside the tags. But for this, it doesn't seem to work:

$description = $item->getElementsByTagName('desc');
$description = $description->item(0)->firstChild->nodeValue;


<sql:time>21:00</sql:time>
<sql:event> Empty </sql:event>
<sql:desc>This field is empty</sql:desc>

thanks

+2  A: 

Use the namespace equvilant of the function you are currently using:

The colon in the element name deneotes the namespace it is within. So you will get the information out of sql:time by calling as:

$description = $item->getElementsByTagNameNS( 'sql', 'time' );
Kevin Peno
I've tried it, and it didnt work. Is it maybe because its parent tag is just a regular tag? and im running a foreach loop around it
Stephen
It is possible that the namespace isn't properly defined in the XML document, thus why it is not available in dom. See http://us2.php.net/manual/en/domdocument.createattributens.php on how to add the namespace?
Kevin Peno
+2  A: 

Alternatively you could use xpath...

$xp = new DOMXpath($dom);

$xpath->registerNamespace('sql', "http://URI");

$xpath_str = '//sql:desc/text()';

$titles = $xpath->evaluate($xpath_str);

print $titles->item(0)->nodeValue ."\n";

( Assuming I didn't make any mistakes which I might have.. )

http://www.php.net/manual/en/domxpath.registernamespace.php

meder