2010-07-04 143 views
0

我正在使用SimpleXML從外部源解析數據文件。我想拉從結果的縮略圖,它看起來像這樣:SimpleXML不會正確返回數據

<entry> 
     <title>Ball_Punch</title> 
     <author> 
      <name>burningcandle2010</name> 
      <uri>https://www.mochimedia.com/community/profile/burningcandle2010</uri> 
     </author> 
     <link href="http://www.mochimedia.com/games/allout-offsite" rel="alternate" /> 
     <link href="http://games.mochiads.com/c/g/allout-offsite/Ball_Punch.swf" rel="enclosure" type="application/x-shockwave-flash" /> 
     <id>urn:uuid:bf720e45-7ca0-34c7-a63a-0f6f20a4c267</id> 
     <media:player height="470" url="http://games.mochiads.com/c/g/allout-offsite/Ball_Punch.swf" width="798" /> 
     <media:thumbnail height="100" url="http://thumbs.mochiads.com/c/g/allout-offsite/_thumb_100x100.png" width="100" /> 
     <media:title>Ball_Punch</media:title> 
     <media:description>punch your ball</media:description> 
     <media:keywords>other, rhythm</media:keywords> 
     <category term="Puzzles" /> 
     <updated>2010-07-04T08:13:22.571963-08:00</updated> 
     <published>2010-07-04T06:58:55.577826-08:00</published> 
     <summary type="xhtml"> 
      <div xmlns="http://www.w3.org/1999/xhtml"> 
      <a href="http://www.mochimedia.com/games/allout-offsite"> 
       <img class="thumbnail" src="http://thumbs.mochiads.com/c/g/allout-offsite/_thumb_100x100.png" /> 
      </a> 
      <dl> 
       <dt>Tag</dt> 
       <dd class="tag">cdb41e529fbe39bd</dd> 
       <dt>Description</dt> 
       <dd class="description">punch your ball</dd> 
       <dt>Resolution</dt> 
       <dd class="resolution">798x470</dd> 
       <dt>Instructions</dt> 
       <dd class="instructions" /> 
       <dt>Key Mappings</dt> 
       <dd class="key_mappings" /> 
       <dt>Control Scheme</dt> 
       <dd class="control_scheme">{"fire": "left_mouse", "jump": "space", "movement": "mouse"}</dd> 
       <dt>Categories</dt> 
       <dd class="categories">Puzzles</dd> 
       <dt>Keywords</dt> 
       <dd class="keywords">other, rhythm</dd> 
       <dt>Rating</dt> 
       <dd class="rating">Everyone</dd> 
       <dt>Leaderboards</dt> 
       <dd class="leaderboards">False</dd> 
       <dt>Embed</dt> 
       <dd> 
        <code class="embed">&lt;embed src="http://games.mochiads.com/c/g/allout-offsite/Ball_Punch.swf" menu="false" quality="high" width="798" height="470" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /&gt;</code> 
       </dd> 
       <dt>Slug</dt> 
       <dd class="slug">allout-offsite</dd> 
       <dt>Featured</dt> 
       <dd class="recommended">False</dd> 
       <dt>Zip File</dt> 
       <dd class="zip_url">http://games.mochiads.com/c/g/allout-offsite.zip</dd> 
       <dt>SWF file size</dt> 
       <dd class="swf_file_size">184374</dd> 
      </dl> 
     </div> 
    </summary> 
</entry> 

我的代碼是在這裏:

$thumbnail = $game->summary->div->a->img->attributes()->src; 

然而,當我運行此通過的print_r($縮略圖),我得到:

SimpleXMLElement Object 
(
    [0] => DATA_I_WANT 
) 

無論我做什麼,它總是捲起這個或一個空的SimpleXMLElement對象。我試過->src[0], ->src->{'0'}等無濟於事。

回答

2

儘量簡單類型轉換爲字符串:

print_r((string) $thumbnail) 

的SimpleXML有被一點點的黑幕當您嘗試實際使用它收集數據的習慣。我習慣於對所有吐出來的東西進行類型化處理。

祝你好運!

1

只投射到一個字符串。 :]

$thumbnail = (string)$game->summary->div->a->img->attributes()->src;