2015-02-07 68 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