0

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 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

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
It is possible that the namespace isn't properly defined in the XML document, thus why it is not available in dom. See us2.php.net/manual/en/domdocument.createattributens.php on how to add the namespace?

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.