简体   繁体   English

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

[英]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. 令人讨厌的是-我有一个简单的XML元素,我只想提取'_Code'属性。 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 http://php.net/manual/zh/function.simplexml-load-string.php

Use SimpleXMLElement::attributes() 使用SimpleXMLElement :: attributes()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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