【发布时间】:2013-07-29 13:23:51
【问题描述】:
我有一个具有以下节点的 SimpleXML 对象,@attributes。这是simplexml_load_string() 从 USPS 获得的 XML 字符串的结果。
$xml =
SimpleXMLElement Object
(
[@attributes] => Array
(
[CLASSID] => 3
)
[MailService] => Priority Mail Express 1-Day
[Rate] => 19.10
)
我知道您可以执行以下操作
$temp = $xml->attributes(); // will return object with '@attributes' note
$temp = (array)$temp; // now in array form
echo $temp['@attributes']['CLASSID']; // prints 3
$xml->{'Rate'}; // will return the rate (19.10) as a string
有什么特别的原因,你为什么要@attributes 为CLASSID?为什么不将CLASSID 与MailService 或Rate 相同?
【问题讨论】:
-
对不起,我澄清了我的问题。我不是在问如何访问它。我在询问 XML 对象结构背后的原因。如有必要,请删除。
-
XML 的格式不是我们能回答的,你得问 USPS。
-
^,好的,这是真的。我注意到 USPS 处理数据的方式很奇怪。
-
如果您查看前面问题的答案,您应该会发现这正是 SimpleXML 在
var_dump输出 中表示 XML 属性的方式。实际对象中没有@attributes,您无需担心。 -
您的最后一个示例表明您尚未阅读其他问题的答案。您确实不需要需要转换为数组并查找
['@attributes'],您只需键入echo $xml['CLASSID'];。见the "basic usage" in the manual。