【发布时间】:2015-03-27 16:36:31
【问题描述】:
在响应中,我们收到一个 xml 文件,然后转换为 SimpleXMLElement,然后根据需要访问元素和属性。但是,当直接从字符串响应与保存的响应加载 xml 时,我们会得到“尝试获取非对象的属性”。
//This code works
$response = simplexml_load_file( "response.xml" );
var_dump($response);
echo $response->RESPONSE->RESPONSE_DATA->FILE_INFORMATION['Order_Number'];
//Returns
//object(SimpleXMLElement)#153 (4) { ["@attributes"]=> array(1)...the rest of the xml file...
//Order_Number
//This code returns error
$response = simplexml_load_string( $response );
var_dump($response);
echo $response->RESPONSE->RESPONSE_DATA->FILE_INFORMATION['Order_Number'];
//Returns
//object(SimpleXMLElement)#153 (1) { [0]=> string(33864) "" }
//Notice: Trying to get property of non-object in...
使用 simplexml_load_string 而不是 simplexml_load_file 会导致 xml 失败的原因是什么?
这是xml文件的sn-p:
<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE_GROUP>
<RESPONSE>
<RESPONSE_DATA>
<FILE_INFORMATION Order_Number="19222835">
...
</FILE_INFORMATION>
</RESPONSE_DATA>
</RESPONSE>
</RESPONSE_GROUP>
【问题讨论】:
-
你的字符串格式正确吗?发布你的字符串
-
是的,格式正确。 response.xml 等于 $response。我会发布,但它是 50k 行...
-
减少 xml 中的行,如果它与 simplexml_load_file 一起使用,请使用 xml 内容编辑问题
-
@AdrianCidAlmaguer 我将 xml 响应保存到 dB,然后在该 xml 响应上使用 simplexml_load_string,它现在按预期工作。我现在可以使用它,但对我来说仍然没有任何意义。
-
世界是陌生而神秘的;-)