doty58493 2016-01-17 17:53
浏览 20
已采纳

获取xml的状态

I am working with the amazon API and I need to check the status of the XML. For example:

<GetMatchingProductForIdResult status="Success" IdType="UPC" Id="082686068055">

or

<GetMatchingProductForIdResult status="ClientError" IdType="UPC" Id="082686068055">

How would I go about writing a code that checks if the status is not "Success"? The XML looks like this:

<GetMatchingProductForIdResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
  <GetMatchingProductForIdResult status="Success" IdType="UPC" Id="082686068055">
    <Products>
      <Product>
        <Identifiers>
          ... 
        </Identifiers>
        <AttributeSets>
        </AttributeSets>
      </Product>
    </Products>
  </GetMatchingProductForIdResult>

Error:

<GetMatchingProductForIdResult Id="082686035408" IdType="UPC" status="ClientError">
  <Error>
    <Type>Sender</Type>
      <Code>InvalidParameterValue</Code>
      <Message>Invalid UPC identifier 082686035408 for marketplace ATVPDKIKX0DER</Message>
  </Error>
</GetMatchingProductForIdResult>

PHP Code to retrieve the content:

if(isset($items->Products->Product->AttributeSets->children($namespace['ns2'])->ItemAttributes->ListPrice->Amount)) { 
    $amount = $items->Products->Product->AttributeSets->children($namespace['ns2'])->ItemAttributes->ListPrice->Amount;
}else{
    $amount = '0.00';
}

I was able to create this code to get the ID of the product:

//$xml is an open XML file.
$items=$xml->GetMatchingProductForIdResult; 
if(isset($items['Id'])){ 
    $id = $items['Id']; 
}else{
    $id = 'No Id Found';
}

The first tag stays throughout the whole XML file. The tag closes at the end of the file. I am using SimpleXML to open, and get all of the other data needed from the file, but I always have an error when the tags in <AttributeSets> is not valid. I need to find a way to avoid this problem. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dtlh12053 2016-01-17 19:08
    关注

    In fact, there is many ways to go. But as you want just detect whether a error or success occurr:

    <?php 
    
    $xmldata = <<<XML
    <?xml version='1.0' ?>
    <GetMatchingProductForIdResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
        <GetMatchingProductForIdResult status="Success" IdType="UPC" Id="082686068055">
            ....
        </GetMatchingProductForIdResult>
    </GetMatchingProductForIdResponse>
    XML;
    
    $xml = new SimpleXmlElement($xmldata);
    $items = $xml->GetMatchingProductForIdResult;
    $ERROR_FOUND = 'Error' == $items->attributes()->status;
    
    if ($ERROR_FOUND) {
        // do something on error, such as return or exit()...
    } 
    // continue xml data parsing
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line