使用陣列形式存取屬性非常簡單。但是,如果您打算將這些值傳遞給函數,則必須將它們轉換為字串或整數。
<?php
SimpleXMLElement 物件
(
[@attributes] => 陣列
(
[id] => 55555
)
[text] => "hello world"
)
?>
然後使用函數
<?php
函式 xml_attribute($object, $attribute)
{
if(isset($object[$attribute]))
return (string) $object[$attribute];
}
?>
我可以像這樣取得 "id"
<?php
print xml_attribute($xml, 'id'); //印出 "55555"
?>