Php 使用simplexml\u加载\u字符串获取xml属性

Php 使用simplexml\u加载\u字符串获取xml属性,php,xml,simplexml,Php,Xml,Simplexml,可能重复: 我正在使用一些第三方API,它们通过xml以以下形式返回错误: <xml> <status>0</status> <error code="111">Error message text goes here.</error> </xml> '; ?> 输出 $x = '<xml> <status>0</status> <error code="111">Err

可能重复:

我正在使用一些第三方API,它们通过xml以以下形式返回错误:

<xml>
<status>0</status>
<error code="111">Error message text goes here.</error>
</xml>
'; ?> 输出

$x = '<xml>
<status>0</status>
<error code="111">Error message text goes here.</error>
</xml>';

$attributeObject = simplexml_load_string($x)->error['code'];

print_r($attributeObject);
print_r((string) $attributeObject);

我错过什么了吗?是否有办法获得该值,或者有人可以建议其他方法获得该值?

它就在那里,但只是没有显示在
打印\r
输出中。比如:

到目前为止,我们只讨论了读取元素名称及其值的工作。SimpleXML还可以访问元素属性。访问元素的属性,就像访问元素的属性一样

例如:

SimpleXMLElement Object
(
    [0] => 111
)
111
就这样用吧 echo$xml->错误['code']

例如:

拯救了生命。。。。。。。。。。。。。。在`
[@attributes]
的情况下,使用此
$obj->attributes()->status
$x = '<xml>
<status>0</status>
<error code="111">Error message text goes here.</error>
</xml>';

$attributeObject = simplexml_load_string($x)->error['code'];

print_r($attributeObject);
print_r((string) $attributeObject);
SimpleXMLElement Object
(
    [0] => 111
)
111