从 PHP SimpleXML 节点获取实际值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1133931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting actual value from PHP SimpleXML node
提问by James
$value = $simpleXmlDoc->SomeNode->InnerNode;
actually assigns a simplexml object to $value instead of the actual value of InnerNode.
实际上将一个 simplexml 对象分配给 $value 而不是 InnerNode 的实际值。
If I do:
如果我做:
$value = $simpleXmlDoc->SomeNode->InnerNode . "\n";
I get the value. Anyway of getting the actual value without the clumsy looking . "\n"?
我得到了价值。无论如何,在没有笨拙外观的情况下获得实际价值. "\n"?
回答by Greg
Cast as whatever type you want (and makes sense...). By concatenating, you're implicitly casting to string, so
投射为您想要的任何类型(并且有意义......)。通过连接,您隐式转换为字符串,因此
$value = (string) $xml->someNode->innerNode;
回答by David
You don't have to specify innerNode.
您不必指定innerNode.
$value = (string) $simpleXmlDoc->SomeNode;
$value = (string) $simpleXmlDoc->SomeNode;
回答by Pascal MARTIN
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.
或者你可以使用 strval()、intval() 和所有这些——只是可能更慢,因为函数调用。
回答by PatrikAkerstrand
Either cast it to a string, or use it in a string context:
要么将其转换为字符串,要么在字符串上下文中使用它:
$value = (string) $simpleXmlDoc->SomeNode->InnerNode;
// OR
echo $simpleXmlDoc->SomeNode->InnerNode;

