0

我有一个具有以下节点的 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一样MailServiceor Rate

4

1 回答 1

2

节点的属性与其他节点/子节点的处理方式不同。@attributes是指向属性内部表示的链接。

要访问属性,请使用类似

echo $xml->attributes['CLASSID']

正如 IMSop 在下面的评论中指出的那样,访问属性的更好方法是使用数组表示法。例如,

echo $xml['CLASSID']
于 2013-07-29T16:46:05.303 回答