SimpleXMLElement - 尝试获取 属性 个非对象
SimpleXMLElement - Trying to get property of non-object
在响应中,我们收到一个 xml 文件,然后转换为 SimpleXMLElement,然后根据需要访问元素和属性。但是,当 xml 直接从字符串响应而不是从保存的响应加载时,我们得到 "Trying to get property of non-object"。
//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 文件的片段:
<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE_GROUP>
<RESPONSE>
<RESPONSE_DATA>
<FILE_INFORMATION Order_Number="19222835">
...
</FILE_INFORMATION>
</RESPONSE_DATA>
</RESPONSE>
</RESPONSE_GROUP>
这对我有用:
<?php
$response = '<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE_GROUP>
<RESPONSE>
<RESPONSE_DATA>
<FILE_INFORMATION Order_Number="19222835">
...
</FILE_INFORMATION>
</RESPONSE_DATA>
</RESPONSE>
</RESPONSE_GROUP>';
//This code returns error
$response = simplexml_load_string( $response );
var_dump($response);
echo $response->RESPONSE->RESPONSE_DATA->FILE_INFORMATION['Order_Number'];
?>
输出:
object(SimpleXMLElement)#1 (1) {
["RESPONSE"]=>
object(SimpleXMLElement)#2 (1) {
["RESPONSE_DATA"]=>
object(SimpleXMLElement)#3 (1) {
["FILE_INFORMATION"]=>
string(33) "
...
"
}
}
}
19222835
您刚刚忽略了这里的一个小细节。你说的第一部分是正确的:
$response = simplexml_load_file( "response.xml" );
这会从文件中加载 XML 文档。但是,当您查看第二部分时:
$response = simplexml_load_string( $response );
您没有从字符串响应中加载。 $response
表示您刚刚从文件创建的 SimpleXMLElement。比较"correct"的例子是:
$buffer = file_get_contents( "response.xml" );
$response = simplexml_load_string( $buffer );
您可能只是因为变量重用而感到困惑(将相同的命名变量用于两种不同的事物)。
更好的是 var_dump
与 $response->asXML()
核实,因为它会将文档显示为 XML,这会更好地显示您拥有(或不拥有)的内容。
在响应中,我们收到一个 xml 文件,然后转换为 SimpleXMLElement,然后根据需要访问元素和属性。但是,当 xml 直接从字符串响应而不是从保存的响应加载时,我们得到 "Trying to get property of non-object"。
//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 文件的片段:
<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE_GROUP>
<RESPONSE>
<RESPONSE_DATA>
<FILE_INFORMATION Order_Number="19222835">
...
</FILE_INFORMATION>
</RESPONSE_DATA>
</RESPONSE>
</RESPONSE_GROUP>
这对我有用:
<?php
$response = '<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE_GROUP>
<RESPONSE>
<RESPONSE_DATA>
<FILE_INFORMATION Order_Number="19222835">
...
</FILE_INFORMATION>
</RESPONSE_DATA>
</RESPONSE>
</RESPONSE_GROUP>';
//This code returns error
$response = simplexml_load_string( $response );
var_dump($response);
echo $response->RESPONSE->RESPONSE_DATA->FILE_INFORMATION['Order_Number'];
?>
输出:
object(SimpleXMLElement)#1 (1) {
["RESPONSE"]=>
object(SimpleXMLElement)#2 (1) {
["RESPONSE_DATA"]=>
object(SimpleXMLElement)#3 (1) {
["FILE_INFORMATION"]=>
string(33) "
...
"
}
}
}
19222835
您刚刚忽略了这里的一个小细节。你说的第一部分是正确的:
$response = simplexml_load_file( "response.xml" );
这会从文件中加载 XML 文档。但是,当您查看第二部分时:
$response = simplexml_load_string( $response );
您没有从字符串响应中加载。 $response
表示您刚刚从文件创建的 SimpleXMLElement。比较"correct"的例子是:
$buffer = file_get_contents( "response.xml" );
$response = simplexml_load_string( $buffer );
您可能只是因为变量重用而感到困惑(将相同的命名变量用于两种不同的事物)。
更好的是 var_dump
与 $response->asXML()
核实,因为它会将文档显示为 XML,这会更好地显示您拥有(或不拥有)的内容。