SimpleXML/PHP - 无法访问对象
$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = simplexml_load_string($tmp2);
echo $xml->RESPONSE->DATA->HEADER->COLUMN[0];
即使 var_dump 成功,上面也不会输出任何内容:
object(SimpleXMLElement)#2 (1) {
["RESPONSE"]=>
object(SimpleXMLElement)#3 (1) {
["DATA"]=>
object(SimpleXMLElement)#4 (1) {
["HEADER"]=>
object(SimpleXMLElement)#5 (1) {
["COLUMN"]=>
array(2) {
[0]=>
string(8) "interval"
[1]=>
string(13) "creative_name"
}
}
}
}
}
谢谢
$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = simplexml_load_string($tmp2);
echo $xml->RESPONSE->DATA->HEADER->COLUMN[0];
The above won't output anything, even though a var_dump is sucessful:
object(SimpleXMLElement)#2 (1) {
["RESPONSE"]=>
object(SimpleXMLElement)#3 (1) {
["DATA"]=>
object(SimpleXMLElement)#4 (1) {
["HEADER"]=>
object(SimpleXMLElement)#5 (1) {
["COLUMN"]=>
array(2) {
[0]=>
string(8) "interval"
[1]=>
string(13) "creative_name"
}
}
}
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是因为 SimpleXML 需要精确的类型转换,否则您会遇到像这样的荒谬的事情 - var_dump 将输出您想要的内容,而 echo 不会。你总是需要这样做,还有更糟糕的错误,比如
echo ceil($simplexml->someNumber)
将输出7
,如果数字例如7.85
等等。尝试改为:
This is because SimpleXML requires exact typecasting or you'll get riddiculous things happening like this - var_dump will output what you want, echo won't. You always need to do this, there are even worse bugs, like
echo ceil($simplexml->someNumber)
will output7
if the number is for example7.85
and so on.Try instead:
XML 输入有效,我想问题出在您的 PHP 设置(旧版本或有错误的版本)上。这是我的机器上的输出(PHP 5.3.8):
问题更新后编辑:
这确实必须是您的 PHP 版本,这又是我机器上的输出:
The XML input is valid, I suppose the problem is with your PHP setup (old or buggy version). This is the output on my machine (PHP 5.3.8):
EDIT after update of question:
This really must be your PHP version, this is the output on my machine again:
您可以尝试抑制错误,然后迭代它们并检查是否可以修复它们,如解释的 此处,因为我在示例 XML 中看不到错误。
You could try to suppress the errors and then iterate over them an check if you can fix them, like explained HERE, because I can't see an error in the example XML.
这将是真正的方法,因此无论 XML 输入是什么,您都可以对其进行配置,使其看起来更像这样,这样它就不是“字符串”,而是实际上制定的 XML 信息。
would be the real way to do it, so whatever the XML input is, you would take and configure it so that it looks more like that so that it's not a 'string' but it's actually formulated XML information.