2015-02-07 67 views
-1

進行連接如何使用PHP從此XML文件讀取數據?

$Game_ID   = $Game_Search->Game[$i]->id; 
$Game_Info_URL  = 'http://thegamesdb.net/api/GetGame.php?id='.$Game_ID; 
$Game_Info_Output = simplexml_load_file($Game_Info_URL); 

檢索數據實例

$Game_Info_Images = $Game_Info_Output->Game->Images; 

對於這個問題請參考this URL,我想獲得的遊戲 - >圖像 - >箱藝術A面和Side B.我怎麼稱呼它?

XML文檔(只是必需的字段)

<Data> 
    <baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl> 
    <Game> 
     <Images> 
      <boxart side="back" width="1518" height="2148" thumb="boxart/thumb/original/back/90-1.jpg">boxart/original/back/90-1.jpg</boxart> 
      <boxart side="front" width="1530" height="2148" thumb="boxart/thumb/original/front/90-1.jpg">boxart/original/front/90-1.jpg</boxart> 
     </Images> 
    </Game> 
</Data> 

回答

0

XML文檔(只是必需的字段)

<Data> 
    <baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl> 
    <Game> 
     <Images> 
      <boxart side="back" width="1518" height="2148" thumb="boxart/thumb/original/back/90-1.jpg">boxart/original/back/90-1.jpg</boxart> 
      <boxart side="front" width="1530" height="2148" thumb="boxart/thumb/original/front/90-1.jpg">boxart/original/front/90-1.jpg</boxart> 
     </Images> 
    </Game> 
</Data> 

要讀取側= 「前」 寬度= 「1530」 ...簡單地使用;

boxart["Attribute_Name"] 

例子:

Game->Images->boxart[$b]["side"] // Gets the side value front/back 
Game->Images->boxart[$b]["width"] // gets the width value 
Game->Images->boxart[$b]["height"] // gets the height value 
Game->Images->boxart[$b]["thumb"] // gets the thumb value 
0

的DomDocument和/或XPath:

$dom = new DOMDocument(); 
$dom->load('http://thegamesdb.net/api/GetGame.php?id=90'); 
$xpath  = new DOMXPath($dom); 
$baseImgUrl = $xpath->query('//baseImgUrl')->item(0)->nodeValue; 
$boxartBackSide = $xpath->query('//Game/Images/boxart[@side="back"]') 
    ->item(0)->nodeValue; 
$boxartFrontSide = $xpath->query('//Game/Images/boxart[@side="front"]') 
    ->item(0)->nodeValue; 
+0

我從來沒有做過的DomDocument所以我會等待,看看是否有人對此有何評論爲我,我自己的答案似乎更簡單... – 2015-02-09 04:41:02

+0

@TimMarshall,是的,你是對的! – felipsmartins 2015-02-09 16:21:07