denormalizer
denormalizer

Reputation: 2214

How to get a simple SimpleXMLElement value

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

Upvotes: 1

Views: 345

Answers (2)

codaddict
codaddict

Reputation: 455020

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"

Upvotes: 2

denormalizer
denormalizer

Reputation: 2214

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

$error = $xml;

Thanks for looking :)

Upvotes: 0

Related Questions