2013-05-04 45 views
-1

我有以下的XML数据foreach循环停止的第一个XML元素

<Items> 
    <Request> 
    <IsValid>True</IsValid> 
    <ItemLookupRequest> 
    <Condition>All</Condition> 
    <IdType>ISBN</IdType> 
    <ItemId>0071762345</ItemId> 
    <ResponseGroup>AlternateVersions</ResponseGroup> 
    <SearchIndex>All</SearchIndex> 
    <VariationPage>All</VariationPage> 
    </ItemLookupRequest> 
    </Request> 
    <Item> 
    <ASIN>0071762345</ASIN> 
    <AlternateVersions> 
    <AlternateVersion> 
    <ASIN>B0058O8V9U</ASIN> 
    <Title> 
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks) [Paperback] Dave Kerpen Dave Kerpen 
    </Title> 
    <Binding>Unknown Binding</Binding> 
    </AlternateVersion> 
    <AlternateVersion> 
    <ASIN>B00511ONPG</ASIN> 
    <Title> 
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks) 
    </Title> 
    <Binding>Kindle Edition</Binding> 
    </AlternateVersion> 
    <AlternateVersion> 
    <ASIN>0071813721</ASIN> 
    <Title> 
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks) 
    </Title> 
    <Binding>Hardcover</Binding> 
    </AlternateVersion> 
    </AlternateVersions> 
    </Item> 
    <Item> 
    <ASIN>B00511ONPG</ASIN> 
    <AlternateVersions> 
    <AlternateVersion> 
    <ASIN>0071762345</ASIN> 
    <Title> 
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (And Other Social Networks) 
    </Title> 
    <Binding>Paperback</Binding> 
    </AlternateVersion> 
    <AlternateVersion> 
    <ASIN>B0058O8V9U</ASIN> 
    <Title> 
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks) [Paperback] Dave Kerpen Dave Kerpen 
    </Title> 
    <Binding>Unknown Binding</Binding> 
    </AlternateVersion> 
    <AlternateVersion> 
    <ASIN>0071813721</ASIN> 
    <Title> 
    Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks) 
    </Title> 
    <Binding>Hardcover</Binding> 
    </AlternateVersion> 
    </AlternateVersions> 
    </Item> 
    </Items> 
    </ItemLookupResponse> 

,我使用的代码搜索每一个绑定元素,然后做的东西与它这样的

foreach($xml->Items->Item->AlternateVersions->AlternateVersion->Binding as $BookBinding) { //loop through the xml data to find the correct ASIN for the kindle edition 
    foreach ($xml->Items->Item->AlternateVersions->AlternateVersion->ASIN as $Kindlestring) 
    { 
     var_dump ($BookBinding); 
     if (preg_match('/Kindle Edition/i',$BookBinding)) 
     { 
      //do stuff 
     } 
    } 
} 

但只获得第一次迭代$ Binding和$ ASIN而不是所有4个元素 var_dump的输出是“Unknown Binding”& B0058O8V9U

+0

这不是一个*,而是*,这是正确的。与xpath查询不同,您在这里使用的迭代器每个“仅包含”一个元素。您可能希望使用xpath查询liek'/ */*/Item/*/AlternateVersion/Binding'等代替。 – hakre 2013-05-04 19:40:40

+0

看起来你有一些无效的XML。尝试通过http://www.w3schools.com/xml/xml_validator.asp运行它并在再次尝试之前更正相关错误。 – 2013-05-04 19:34:20

+0

使用''ASIN [follow-sibling :: Binding =“Kindle Edition”]' – Gordon 2013-05-04 20:10:13

回答

0

这可以帮助你

foreach($xml->Items->Item as $item){ 
    foreach($item->AlternateVersions->AlternateVersion as $alt_version){ 
     var_dump($alt_version->Binding); 
     var_dump($alt_version->ASIN); 
     ... 
    } 
} 

首先迭代throught '项目',然后 'AlternateVersion'。

相关问题