2011-12-19 65 views
0

我有以下SimpleXML的循環條目

<entry> 
     <id></id> 
     <title></title> 
     <dc:identifier></dc:identifier> 
     <dc:identifier></dc:identifier> 
     <dc:relation></dc:relation> 
     <dc:publisher></dc:publisher> 
     <dc:language xsi:type="dcterms:ISO639-2"></dc:language> 
     <dcterms:issued></dcterms:issued> 
     <updated></updated> 
     <rights></rights> 
     <author> 
      <name></name> 
      <uri></uri> 
     </author> 
     <link rel="alternate" href="" type="text/html"/> 
     <link rel="http://opds-spec.org/image" href="" type="image/jpeg"/> 
     <link rel="http://opds-spec.org/image/thumbnail" href="" type="image/jpeg"/> 
     <link rel="http://opds-spec.org/acquisition/buy" href="" type="application/epub+zip"> 
      <opds:price currencycode="EUR"></opds:price> 
     </link> 
     <summary type="text"></summary> 
</entry> 
<entry> 
     <id></id> 
     <title></title> 
     <dc:identifier></dc:identifier> 
     <dc:identifier></dc:identifier> 
     <dc:relation></dc:relation> 
     <dc:publisher></dc:publisher> 
     <dc:language xsi:type="dcterms:ISO639-2"></dc:language> 
     <dcterms:issued></dcterms:issued> 
     <updated></updated> 
     <rights></rights> 
     <author> 
      <name></name> 
      <uri></uri> 
     </author> 
     <link rel="alternate" href="" type="text/html"/> 
     <link rel="http://opds-spec.org/image" href="" type="image/jpeg"/> 
     <link rel="http://opds-spec.org/image/thumbnail" href="" type="image/jpeg"/> 
     <link rel="http://opds-spec.org/acquisition/buy" href="" type="application/epub+zip"> 
      <opds:price currencycode="EUR"></opds:price> 
     </link> 
     <summary type="text"></summary> 
</entry> 
...... 

的XML解析所有(的SimpleXML)

現在我想在time.So拿到一個項目我路過一個變種,當我可以罰款解析它我的文件中像

file.php?start=1 
file.php?start=2 
etc 

,我試圖訪問當前項目

$xml->entry[$start] returns NULL 

OR

$xml->entry->{$start} returns NULL 

但都返回NULL

我試圖直接訪問它

$xml->entry[1] 
$xml->entry->{1} 

其做工精細

我失去了somehthing?

回答

1

$ _GET ['start']可能不是正確的變量類型,請嘗試將類型轉換爲整數。

+0

感謝沒有想到這個 – ntan 2011-12-19 07:04:25

1
<?php 
$xml = simplexml_load_file('simplexml.php'); 

$id = '1'; 
print_r($xml->entry[$id]); 
?> 

結果:

NULL

<?php 
$xml = simplexml_load_file('simplexml.php'); 

$id = '1'; 
$id = (int)$id; 

print_r($xml->entry[$id]); 

?> 

結果:

SimpleXMLElement對象 ( [條目] => SimpleXMLElement對象 ( [id] => 2 [標題] =>其他標題 ) )