views:

21

answers:

2

I'm aware of how to drill down into the nodes of an xml document as described here:

http://www.php.net/manual/en/simplexml.examples-basic.php

but am at a loss on how to extract the value in the following example

$xmlStr = '<Error>Hello world. There is an Error</Error>';
$xml = simplexml_load_string($xmlStr);

I'm sure it's something quite simple.

Thanks in advance

A: 

What do you know. The value of the tag is just:

$error = $xml;

Thanks for looking :)

superspace
+2  A: 

simplexml_load_string returns an object of type SimpleXMLElement whose properties will have the data of the XML string.

In your case there is no opening <xml> and closing </xml> tags, which every valid XML should have.

If these were present then to get the data between <Error> tags you can do:

$xmlStr = '<xml><Error>Hello world. There is an Error</Error></xml>';
$xml = simplexml_load_string($xmlStr);
echo $xml->Error; // prints "Hello world. There is an Error"
codaddict
+1: For being thorough.
shamittomar
Yeap, that makes sense. The original xml is invalid as it missed the xml tags hence I couldn't do $xml->Error in my original question. Thanks for your answer.
superspace
In the case where you are dealing with invalid xml (as I was), echo $xml; would suffice if you wanted to echo out the error :)
superspace
@superspace: You are correct. But its better to keep XML as valid :)
codaddict