duanqiao1947 2012-08-13 18:03
浏览 36
已采纳

SimpleXML检查存在SimpleXML对象

I am using an if statement to check whether an XML object exists, but it does not work well as I get trying to get property of non-object error

below is the code

foreach($resp->GetLowestOfferListingsForASINResult as $product){
   $isbn = "";
   $price = "";
   $condition = "";

   if($product->Product->Identifiers->MarketplaceASIN->ASIN){
      $isbn = (string) $product->Product->Identifiers->MarketplaceASIN->ASIN;
   }

   if($product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount){
      $price = (float) $product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount;
   }

   if($product->Product->LowestOfferListings->LowestOfferListing->Qualifiers->ItemSubcondition){
      $condition = (string) $product->Product->LowestOfferListings->LowestOfferListing->Qualifiers->ItemSubcondition;
   }
}

I have tried using

if(!property_exists($product, 'Amount')){
   $price = (float) $product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount;
}

but that does not work, what should I be using

xml returned result (if price exists)

<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestOfferListingsForASINResult ASIN="000726965X" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
        <ASIN>000726965X</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <LowestOfferListings>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>Used</ItemCondition>
          <ItemSubcondition>Mint</ItemSubcondition>
          <FulfillmentChannel>Amazon</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>500720</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>12.65</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>12.65</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>Used</ItemCondition>
          <ItemSubcondition>Acceptable</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>73208</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>15.41</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>12.61</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>2.80</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>Used</ItemCondition>
          <ItemSubcondition>Mint</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>2125</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>22.80</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>20.00</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>2.80</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>Used</ItemCondition>
          <ItemSubcondition>Mint</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>90-94%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>68301</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>23.14</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>20.34</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>2.80</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>Used</ItemCondition>
          <ItemSubcondition>Mint</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>False</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>90-94%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>4114</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>25.39</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>22.59</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>2.80</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>Used</ItemCondition>
          <ItemSubcondition>Mint</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>34661</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>29.20</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>26.40</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>2.80</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>Used</ItemCondition>
          <ItemSubcondition>VeryGood</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>False</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>49504</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>33.16</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>30.36</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>GBP</CurrencyCode>
            <Amount>2.80</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
    </LowestOfferListings>
  </Product>
</GetLowestOfferListingsForASINResult>
<ResponseMetadata>
  <RequestId>09a1deae-94ec-4661-95ae-49567ab00eb8</RequestId>
</ResponseMetadata>
</GetLowestOfferListingsForASINResponse>

xml returned result (if price does not exist)

<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestOfferListingsForASINResult ASIN="0957143826" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
        <ASIN>0957143826</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <LowestOfferListings/>
  </Product>
</GetLowestOfferListingsForASINResult>
<ResponseMetadata>
  <RequestId>13918cb9-69e5-4445-ad24-70f6fda06f19</RequestId>
</ResponseMetadata>
</GetLowestOfferListingsForASINResponse>
  • 写回答

1条回答 默认 最新

  • dqrzot2791 2012-08-18 22:24
    关注

    Your problem here is that you are testing too far into the chain of -> operators:

    if ( $product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount )
    

    In order to even test this, PHP must evaluate each of the -> operators in turn

    1. $product->Product [OK]
    2. $product->Product->LowestOfferListings [OK]
    3. $product->Product->LowestOfferListings->LowestOfferListing [An empty set if there are no prices]
    4. $product->Product->LowestOfferListings->LowestOfferListing->Price [Undefined]
    5. After this, you're already too late.

    Additionally, the SimpleXML object won't evaluate to false even if it's an empty list, so you do need to use isset. In order to spot the no-price situation, you therefore need this:

    if ( isset($product->Product->LowestOfferListings->LowestOfferListing) )
    

    [Note: I haven't actually tested the above, so I may have gone wrong in some specific, but I'm pretty sure this is your basic problem.]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊