2012-05-10 84 views
18

的XML我正在讀看起來像這樣:PHP的SimpleXML +獲取屬性

<show id="8511"> 

    <name>The Big Bang Theory</name> 
    <link>http://www.tvrage.com/The_Big_Bang_Theory</link> 
    <started>2007-09-24</started> 
    <country>USA</country> 

    <latestepisode> 
     <number>05x23</number> 
     <title>The Launch Acceleration</title> 
    </latestepisode> 

</show> 

要獲得(例如)在最新一集的數量,我會做:

$ep = $xml->latestepisode[0]->number; 

該作品正好。但是,如何從<show id="8511">獲得ID?

我已經試過類似:

$id = $xml->show; 
$id = $xml->show[0]; 

但沒有奏效。

更新

我的代碼片段:

$url = "http://services.tvrage.com/feeds/episodeinfo.php?show=".$showName; 
$result = file_get_contents($url); 
$xml = new SimpleXMLElement($result); 

//still doesnt work 
$id = $xml->show->attributes()->id; 

$ep = $xml->latestepisode[0]->number; 

echo ($id); 

大利。 XML:

http://services.tvrage.com/feeds/episodeinfo.php?show=The.Big.Bang.Theory 
+0

http://php.net/ simplexml.examples-basic – hakre

+0

可能的重複[從SimpleXML訪問@attribute](http://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml) - 供參考。 – hakre

+0

請參閱: http://stackoverflow.com/questions/10537657/php-simplexml-get-attribute/19289857#19289857 –

回答

31

這應該工作。

$id = $xml["id"]; 

您的XML根成爲SimpleXML對象的根;你的代碼通過'show'的名字來調用一個chid root,這個名字不存在。

您也可以使用這個鏈接一些教程:http://php.net/manual/en/simplexml.examples-basic.php

+2

這似乎是個竅門!非常感謝! (我應該花更多的時間來看看下一個例子:() – Andrej

12

您需要使用attributes

我認爲這應該工作

$id = $xml->show->attributes()->id; 
+0

這不會工作......'show'是默認的根...他需要扭曲(xml) – Baba

+0

不幸的是,我不斷收到警告:main()[function.main]:節點不再存在於....' – Andrej

7

您需要使用attributes()得到的屬性。

$id = $xml->show->attributes()->id; 

你也可以這樣做:

$attr = $xml->show->attributes(); 
$id = $attr['id']; 

或者你可以試試這個:

$id = $xml->show['id']; 

望着編輯您的問題(<show>是你的根元素),試試這個:

$id = $xml->attributes()->id; 

$attr = $xml->attributes(); 
$id = $attr['id']; 

OR

$id = $xml['id']; 
+0

第一個和第二個例子不幸輸出'Warning:main()[function.main ]:節點不再存在....',而最後一個不顯示任何東西。 – Andrej

+0

@Andrej:檢查我的編輯。 –

+0

它的作品,讓我知道,如果你也編輯@山姆的答案,因爲我不會改變正確的答案,並選擇你的現在。 – Andrej

0

您需要正確地格式化XML,讓它有examply使用<root></root><document></document>什麼..看到XML規範和實例在http://php.net/manual/en/function.simplexml-load-string.php

$xml = '<?xml version="1.0" ?> 
<root> 
<show id="8511"> 
    <name>The Big Bang Theory</name> 
    <link>http://www.tvrage.com/The_Big_Bang_Theory</link> 
    <started>2007-09-24</started> 
    <country>USA</country> 

    <latestepisode> 
     <number>05x23</number> 
     <title>The Launch Acceleration</title> 
    </latestepisode> 

</show> 
</root>'; 

$xml = simplexml_load_string ($xml); 
var_dump ($xml->show->attributes()->id); 
+0

不幸的是,我已經沒有影響如何格式化xml! – Andrej

+0

我將原始XML源添加到我的文章! – Andrej

0

您正確加載使用的SimpleXML objecto你可以做一個print_r($xml_variable) XML文件後,你可以很容易地找到它的屬性,你可以訪問。正如其他用戶說$xml['id']也爲我工作。

8

這應該工作。 您需要使用的屬性類型(如果蜇值使用(字符串))

$id = (string) $xml->show->attributes()->id; 
var_dump($id); 

或者這樣:

$id = strip_tags($xml->show->attributes()->id); 
var_dump($id); 
3

試試這個

$id = (int)$xml->show->attributes()->id;