41
$value = $simpleXmlDoc->SomeNode->InnerNode;

actually assigns a simplexml object to $value instead of the actual value of InnerNode.

If I do:

$value = $simpleXmlDoc->SomeNode->InnerNode . "\n";

I get the value. Anyway of getting the actual value without the clumsy looking . "\n"?

hakre
  • 193,403
  • 52
  • 435
  • 836
James
  • 6,471
  • 11
  • 59
  • 86
  • 6
    This was answered at http://stackoverflow.com/questions/416548/forcing-a-simplexml-object-to-a-string-regardless-of-context – null Jul 16 '09 at 14:39

4 Answers4

87

Cast as whatever type you want (and makes sense...). By concatenating, you're implicitly casting to string, so

$value = (string) $xml->someNode->innerNode;
Greg
  • 316,276
  • 54
  • 369
  • 333
Greg
  • 10,350
  • 1
  • 26
  • 35
21

You don't have to specify innerNode.

$value = (string) $simpleXmlDoc->SomeNode;

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
David
  • 211
  • 2
  • 2
5

What about using a typecast, like something like that :

$value = (string)$simpleXmlDoc->SomeNode->InnerNode;

See : type-juggling

Or you can probably use strval(), intval() and all that -- just probably slower, because of the function call.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
4

Either cast it to a string, or use it in a string context:

$value = (string) $simpleXmlDoc->SomeNode->InnerNode;
// OR
echo $simpleXmlDoc->SomeNode->InnerNode;

See the SimpleXML reference functions guide

PatrikAkerstrand
  • 45,315
  • 11
  • 79
  • 94