php - 检查金额是否存在

标签 php xml

在搜索亚马逊返回给我的 XML 时,有些产品没有数量或包装尺寸。到目前为止我编写的用于检查的代码是这样的:

if($items->Products->Product->AttributeSets->children('ns2',TRUE)->ItemAttributes->ListPrice->Amount === null){
    $amount = '0.00';
} elseif (isset($items->Products->Product->AttributeSets->children('ns2',TRUE)->ItemAttributes->ListPrice->Amount) === False){
    $amount = $items->Products->Product->AttributeSets->children('ns2',TRUE)->ItemAttributes->ListPrice->Amount;
}else{
    $amount = '0.00';
}

此代码对于所有问题都是相同的。如果我没有在第一个 if 语句中将这些属性设置为 null,则会出现“children() on null”错误。如果未找到属性“金额”,则会出现这种情况。

金额不是属性的示例:

<GetMatchingProductForIdResult Id="082686074551" IdType="UPC" status="Success">
<Products>
  <Product>
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
        <ASIN>B000UUZVI6</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <AttributeSets>
      <ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-US">
        <ns2:Binding>Toy</ns2:Binding>
        <ns2:Brand>Rubie's</ns2:Brand>
        <ns2:Color>White</ns2:Color>
        <ns2:Department>unisex-child</ns2:Department>
        <ns2:Feature>Size : For ages 4 and up</ns2:Feature>
        <ns2:Label>Rubie's</ns2:Label>
        <ns2:Manufacturer>Rubie's</ns2:Manufacturer>
        <ns2:ProductGroup>Toy</ns2:ProductGroup>
        <ns2:ProductTypeName>TOYS_AND_GAMES</ns2:ProductTypeName>
        <ns2:Publisher>Rubie's</ns2:Publisher>
        <ns2:SmallImage>
          <ns2:URL>http://ecx.images-amazon.com/images/I/41u1YTVvESL._SL75_.jpg</ns2:URL>
          <ns2:Height Units="pixels">75</ns2:Height>
          <ns2:Width Units="pixels">67</ns2:Width>
        </ns2:SmallImage>
        <ns2:Studio>Rubie's</ns2:Studio>
        <ns2:Title>Child Rose Print White Tights Stocking Costume</ns2:Title>
      </ns2:ItemAttributes>
    </AttributeSets>
    <Relationships/>
    <SalesRankings/>
  </Product>
  <Product>
</GetMatchingProductForIdResult>

使用金额属性:

<GetMatchingProductForIdResult Id="082686075824" IdType="UPC" status="Success">
<Products>
  <Product>
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
        <ASIN>B001R4FMEW</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <AttributeSets>
      <ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-US">
        <ns2:Binding>Toy</ns2:Binding>
        <ns2:Brand>Rubie's</ns2:Brand>
        <ns2:Color>Black</ns2:Color>
        <ns2:Department>teen-girls</ns2:Department>
        <ns2:Feature>Fun costumes for kids and adults</ns2:Feature>
        <ns2:Feature>Whether it's for Halloween, a themed party, or even for giggles</ns2:Feature>
        <ns2:Feature>Beautiful colors, hand-wash needed, excellent for dress up</ns2:Feature>
        <ns2:ItemDimensions>
          <ns2:Height Units="inches">8.00</ns2:Height>
          <ns2:Length Units="inches">15.00</ns2:Length>
          <ns2:Width Units="inches">8.00</ns2:Width>
        </ns2:ItemDimensions>
        <ns2:Label>Pro-Motion Distributing - Direct</ns2:Label>
        <ns2:ListPrice>
          <ns2:Amount>3.99</ns2:Amount>
          <ns2:CurrencyCode>USD</ns2:CurrencyCode>
        </ns2:ListPrice>
        <ns2:Manufacturer>Pro-Motion Distributing - Direct</ns2:Manufacturer>
        <ns2:ManufacturerMaximumAge Units="months">96.0</ns2:ManufacturerMaximumAge>
        <ns2:ManufacturerMinimumAge Units="months">18.0</ns2:ManufacturerMinimumAge>
        <ns2:Model>RUB7582ACC</ns2:Model>
        <ns2:PackageDimensions>
          <ns2:Height Units="inches">0.50</ns2:Height>
          <ns2:Length Units="inches">8.00</ns2:Length>
          <ns2:Width Units="inches">6.00</ns2:Width>
          <ns2:Weight Units="pounds">0.20</ns2:Weight>
        </ns2:PackageDimensions>
        <ns2:PackageQuantity>1</ns2:PackageQuantity>
        <ns2:PartNumber>RUB7582ACC</ns2:PartNumber>
        <ns2:ProductGroup>Toy</ns2:ProductGroup>
        <ns2:ProductTypeName>TOYS_AND_GAMES</ns2:ProductTypeName>
        <ns2:Publisher>Pro-Motion Distributing - Direct</ns2:Publisher>
        <ns2:Size>Teen</ns2:Size>
        <ns2:SmallImage>
          <ns2:URL>http://ecx.images-amazon.com/images/I/41A%2BY200WLL._SL75_.jpg</ns2:URL>
          <ns2:Height Units="pixels">75</ns2:Height>
          <ns2:Width Units="pixels">34</ns2:Width>
        </ns2:SmallImage>
        <ns2:Studio>Pro-Motion Distributing - Direct</ns2:Studio>
        <ns2:Title>Rubie's Costume Co Skull Tights-White/Black Costume</ns2:Title>
        <ns2:Warranty>30 day warranty against manufacturer defects</ns2:Warranty>
      </ns2:ItemAttributes>
    </AttributeSets>
    <Relationships/>
    <SalesRankings>
      <SalesRank>
        <ProductCategoryId>toy_display_on_website</ProductCategoryId>
        <Rank>1121721</Rank>
      </SalesRank>
      <SalesRank>
        <ProductCategoryId>251975011</ProductCategoryId>
        <Rank>7203</Rank>
      </SalesRank>
      <SalesRank>
        <ProductCategoryId>251940011</ProductCategoryId>
        <Rank>10177</Rank>
      </SalesRank>
    </SalesRankings>
  </Product>
</Products>
</GetMatchingProductForIdResult>

我不明白问题可能是什么。我很感谢您提前提供的帮助。

最佳答案

失败是因为您正在检查另一个不存在的节点后面的节点。

在一个示例中,节点“ListPrice”不存在,但您正在检查“ListPrice->amount”是否为 null,因此会引发异常。

if($child->Products->Product->AttributeSets->children('ns2',TRUE)->ItemAttributes->ListPrice->Amount === null){
    $amount = '0.00';
} elseif (isset($child->Products->Product->AttributeSets->children('ns2',TRUE)->ItemAttributes->ListPrice->Amount) === False){
    $amount = $child->Products->Product->AttributeSets->children('ns2',TRUE)->ItemAttributes->ListPrice->Amount;
}else{
    $amount = '0.00';
}

因此,如果您想避免失败,请尝试检查拳头:

if isset( $child->.....->ListPrice) {

  if isset( $child->....->ListPrice->amount ) { 
          // Here you are sure amount exists
           $amount = $child->Products->Product->AttributeSets->children('ns2',TRUE)->ItemAttributes->ListPrice->Amount;
  } else { 
          // Amount does not exists
          $amount = 0.00;
  }

} else {
     // Amount does not exists
     $amount = 0.00;
}

关于php - 检查金额是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34843974/

相关文章:

c# - LINQ 选择具有名称的新类

sql - 将xml转换为表

php - 如何确定一个单词是英语还是任何其他语言

php - 所有模型的 Laravel 存储库模式

php - 使用 DMZ Data Mapper 连接到多个数据库

java - 将空标签解码到集合的新实例中

xml - 如何在 XSLT 中使用 for 循环并根据迭代获取节点值

php - 在离线linux服务器上安装joomla 3尚未完成

php - 路由 [verification.resend] 未定义

python - 使用 lxml 在 python 中解析多个命名空间 XML