Getting actual value from PHP SimpleXML node

2019-01-11 12:35发布

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

标签: php simplexml
4条回答
做个烂人
2楼-- · 2019-01-11 12:47

You don't have to specify innerNode.

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

查看更多
Emotional °昔
3楼-- · 2019-01-11 12:50

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

$value = (string) $xml->someNode->innerNode;
查看更多
我命由我不由天
4楼-- · 2019-01-11 12:51

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.

查看更多
We Are One
5楼-- · 2019-01-11 13:00

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

查看更多
登录 后发表回答