Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting actual value from PHP SimpleXML node [duplicate]

Tags:

php

simplexml

$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"?

like image 642
James Avatar asked Jul 15 '09 20:07

James


4 Answers

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

$value = (string) $xml->someNode->innerNode;
like image 146
Greg Avatar answered Oct 05 '22 07:10

Greg


You don't have to specify innerNode.

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

like image 44
David Avatar answered Oct 05 '22 07:10

David


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.

like image 33
Pascal MARTIN Avatar answered Oct 05 '22 08:10

Pascal MARTIN


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

like image 38
PatrikAkerstrand Avatar answered Oct 05 '22 07:10

PatrikAkerstrand