2016-01-28 67 views
3

我正在使用YouTube API。我得到了以下API的結果:訪問xml對象

SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [rel] => alternate 
      [href] => http://www.youtube.com/watch?v=blabla 
     ) 

) 

我很困惑這個對象。我想訪問@attributes。我該怎麼做?

+0

var_dump($ xml - > {'@ attributes'}); $ xml是你的變量,你保存xml – devpro

+1

你不能只是谷歌這個? ['php simplexml get attributes'](https://www.google.com.ph/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=php%20simplexml%20get%20attribute) – Ghost

+0

@Ghost我得到了這個$ xml-> attributes() - > href。謝謝 –

回答

2

print_r輸出的@attributes部分只是元素的屬性,可通過$obj['attrname']訪問。

<?php 
$obj = new SimpleXMLElement('<foo rel="alternate" href="http://www.youtube.com/watch?v=blabla" />'); 
print_r($obj); // to verify that the sample data fits your actual data 

echo $obj['rel'], ' | ', $obj['href']; 

prints

SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [rel] => alternate 
      [href] => http://www.youtube.com/watch?v=blabla 
     ) 

) 
alternate | http://www.youtube.com/watch?v=blabla 

Example #5 Using attributes看到SimpleXML的文檔。