【问题标题】:Parsing XML using a for loop in php在 php 中使用 for 循环解析 XML
【发布时间】:2012-09-30 05:47:10
【问题描述】:

我正在尝试解析 XML 文档。当我查看 var_dump 时,我可以获得所有信息,但我需要做的是从文档中提取 Id 和图像 url。我尝试使用 for each 循环,它会以正确的次数通过循环,但我需要的 Id 号永远不会改变。 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'] . "')");

循环时如何获取每个单独的 ID?

【问题讨论】:

  • 你试过了吗? foreach($parsed_xml->GetMatchingProductForIdResult as $id => $item )
  • 看看simplexmlparser:php.net/manual/en/simplexml.examples.php。然后,您可以使用 Xpath 从已解析的文档中获取数据
  • @HamZaDzCyber​​DeV - 是的,我已经尝试过了,得到的结果与我发布的结果相同。感谢您尝试帮助我

标签: php xml xml-parsing


【解决方案1】:

经过多次尝试和错误的最终解决方案是:

    $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 语句之后使用 $item。

【讨论】:

    猜你喜欢
    • 2012-02-21
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多