从PHP中的简单XML元素中提取属性

时间:2012-04-25 18:02:44

标签: php xml

推动我疯狂 - 我有一个简单的XML元素,我只想提取' _Code'属性。我该怎么做?

<?php

$responseCode = "<STATUS _Condition='FAILURE' _Code='0705' _Description='Search failed subject not found' />";
$xml = simplexml_load_string($responseCode);

print_r($xml);

$code = $xml=>@attributes=>_Code; // Parse error
$code = $xml['@attributes']['_Code']; // Returns blank
echo "CODE = ".(string)$code; 

?>
  

CODE =

http://php.net/manual/en/function.simplexml-load-string.php

1 个答案:

答案 0 :(得分:1)

使用SimpleXMLElement::attributes()

$attrs = $xml->attributes();
$code = $attrs['_Code'];