XML 解析 的 內容會回傳一個物件XML; $xml = simplexml_load_string($string); print_r($xml); ?> Test Jon Tsung
SimpleXMLElement Object
(
[@attributes] => Array
(
[responsecode] => 200
)
[result] => SimpleXMLElement Object
(
[@attributes] => Array
(
[count] => 10
[start] => 0
[totalhits] => 13304
)
[title] => Test
[from] => Jon
[to] => Tsung
)
)
如何取用此物件回傳的值取得 result 下的 title
$xml->result->title; // Test (object)
建議: (string)$xml->result->title; // 強迫轉換成字串
取得屬性的值(@attributes)
$xml->result->attributes()->totalhits; // 13304 (object), 一樣建議於前面加 (string)
或
$result_attr = $xml->result->attributes();
$result_attr['totalhits']; // 13304 (object), 一樣建議於前面加 (string)
原文來源:http://blog.longwin.com.tw/2009/10/php-simplexml-parse-attr-content-2009/
沒有留言:
張貼留言