简体   繁体   中英

Extract attribute from simple XML element in PHP

Driving me bonkers- I've got a simple XML element, and I just want to extract the '_Code' attribute. How would I do it?

<?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

Use SimpleXMLElement::attributes()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM