2012-10-09 7 views
1

XML 문서를 구문 분석하려고합니다. 내가 var_dump를 볼 때 나는 모든 정보를 얻을 수 있지만, 내가해야 할 일은 문서에서 ID와 이미지 URL을 꺼내는 것이다. 각 루프에 대해 a를 사용하려고 시도했지만 올바른 횟수만큼 루프를 통과 할 것이지만 ID 번호는 절대로 변경하지 않아도됩니다. XML 문서입니다 : 내가 간결을 삭감했다PHP에서 for 루프를 사용하여 XML 구문 분석

<?xml version="1.0"?> 
<GetMatchingProductForIdResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"> 
<GetMatchingProductForIdResult Id="9781124028491" IdType="ISBN" status="ClientError"> 
<Error> 
<Type>Sender</Type> 
<Code>InvalidParameterValue</Code> 
<Message>Invalid ISBN identifier 9781124028491 for marketplace ATVPDKIKX0DER</Message> 
</Error> 
</GetMatchingProductForIdResult> 
<GetMatchingProductForIdResult Id="9780030114687" IdType="ISBN" status="Success"> 
<Products xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"> 
<Product> 
    <Identifiers> 
    <MarketplaceASIN> 
     <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
     <ASIN>0030114683</ASIN> 
    </MarketplaceASIN> 
    </Identifiers> 
    <AttributeSets> 
    <ns2:ItemAttributes xml:lang="en-US"> 
     <ns2:Author>Turk, Jonathan.</ns2:Author> 
     <ns2:Binding>Unknown Binding</ns2:Binding> 
     <ns2:Label>Saunders College Publishers, USA</ns2:Label> 
     <ns2:Manufacturer>Saunders College Publishers, USA</ns2:Manufacturer> 
     <ns2:ProductGroup>Book</ns2:ProductGroup> 
     <ns2:ProductTypeName>BOOKS_1973_AND_LATER</ns2:ProductTypeName> 
     <ns2:PublicationDate>2004-12-07</ns2:PublicationDate> 
     <ns2:Publisher>Saunders College Publishers, USA</ns2:Publisher> 
     <ns2:SmallImage> 
     <ns2:URL>http://g-ecx.images-amazon.com/images/G/01/x-site/icons/no-img-sm._V192198896_.gif</ns2:URL> 
     <ns2:Height Units="pixels">40</ns2:Height> 
     <ns2:Width Units="pixels">60</ns2:Width> 
     </ns2:SmallImage> 
     <ns2:Studio>Saunders College Publishers, USA</ns2:Studio> 
     <ns2:Title>Introduction to Environmental Studies.</ns2:Title> 
    </ns2:ItemAttributes> 
    </AttributeSets> 
    <Relationships/> 
    <SalesRankings/> 
</Product> 

. 내 PHP는 이것이다 : 루프를 통과 할 때

foreach($parsed_xml->GetMatchingProductForIdResult as $item) { 
//$ean =$parsed_xml->GetMatchingProductForIdResult->attributes()->Id; var_dump($ean);//->attributes()->Id 
$current = $parsed_xml->GetMatchingProductForIdResult->Products; 

     print_r(" passed the foreach statement "); 

//$status = $parsed_xml->GetMatchingProductForIdResult->attributes()->status; 

    //$isbn13 = $ean;print_r($isbn13); 
     if(isset($parsed_xml->GetMatchingProductForIdResult, $current, $current->Product, $current->Product->AttributeSets)){ 
     $amazonResult = array(
          'isbn' => $parsed_xml->GetMatchingProductForIdResult->attributes()->Id,//$isbn13, 
      'ImageURL' => str_replace('SL75','SL200',$current->Product->AttributeSets->children('ns2', true)->ItemAttributes->SmallImage->URL), 
          ); 
    print_r(" Success was true "); 

    } else { 

$amazonResult = array(
          'isbn' => $parsed_xml->GetMatchingProductForIdResult->attributes()->Id,//$isbn13, 
      'ImageURL' => "Jim", 
          ); 
    print_r(" Success was false "); 
}  
    //update image in images table 
    print_r(" at the insert statement "); 
$conn->query("INSERT INTO images (isbn, image) VALUES ('" . $amazonResult['isbn'] . "', '" . $amazonResult['ImageURL'] . "')"); 

가 어떻게 각각의 아이디를받을 수 있나요?

+0

시도해 보셨습니까? foreach ($ parsed_xml-> GetMatchingProductForIdResult as $ id => $ item) – HamZa

+0

simplexmlparser를보십시오 : http://www.php.net/manual/en/simplexml.examples.php. 그런 다음 Xpath를 사용하여 구문 분석 된 문서의 데이터를 가져올 수 있습니다. – Vikram

+0

@HamZaDzCyberDeV - 예, 시도했으며 게시 한 것과 동일한 결과를 얻습니다. 비록 나를 도우려는 시도에 감사드립니다. – Jim

답변

2

많은 시행 착오 후 최종 솔루션은 다음과 같습니다

$parsed_xml = ProductId_xml($isbn); 


    foreach($parsed_xml->GetMatchingProductForIdResult as $item) { 
$ean =$item->attributes()->Id; 
$current = $item->Products; 

$status = $item->attributes()->status; 

     if (stristr($status, "Success") == true) 
{ 
     $amazonResult = array(
          'isbn' => $ean,//$parsed_xml->GetMatchingProductForIdResult->attributes()->Id, 
      'ImageURL' => str_replace('SL75','SL200',$current->Product->AttributeSets->children('ns2', true)->ItemAttributes->SmallImage->URL), 
          ); 

    } else { 

$amazonResult = array(
          'isbn' => $ean, 
      'ImageURL' => "", 
          ); 

}

나는 foreach 문 후 $ 항목을 사용하는 데 필요한.