General Question

lefteh's avatar

How would I check if an XML element exists in PHP?

Asked by lefteh (9396points) January 28th, 2009

Sorry guys. Same script, different problem. Google is no help.

Here is the script.

The script pulls out the “when” element and its attributes. The problem is that not every entry has a when element. How can I skip these records? I am aware of a hasAttribute function, but I can’t seem to find any equivalent for elements. I’ve played around with several techniques, and nothing has worked.

If these entries are not skipped, the following error occurs:
Fatal error: Call to a member function getAttributeNode() on a non-object

Thanks again..

Observing members: 0 Composing members: 0

2 Answers

funkdaddy's avatar

Wrap it in an if that checks for the existence first…

if($status->item(0)) {
$eventStatus = $status->item(0)>getAttributeNode(“value”)->value;

and run from there, I haven’t actually run your script but that’s the basic concept.

You might also look into the SimpleXML stuff, not quite as powerful but awfully easy to get around and does just about everything you’ll want (then you can use the DOM stuff to fill in the blanks)

smartbob's avatar

agreed with funkdaddy.
foreach($somethings as $something){
do something here
can also do something here for if the value is not set, like assign it if you want

if will occur as long as the value is not null, a whitespace character, or zero. all other values will trigger the if

Answer this question




to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
Knowledge Networking @ Fluther