2012-08-13 61 views
1

我使用if语句来检查XML对象是否存在,但它不能很好地工作,因为我得到trying to get property of non-object errorSimpleXML的检查SimpleXML的对象存在

下面

是代码

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; 
    } 
} 

我曾尝试使用

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

,但不工作,我应该使用

XML返回的结果(如果价格存在)

<?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返回的结果(如果价格不存在)

<?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> 
+0

我也有相同/相似的问题,并被告知我需要检查是否存在子节点。然而,我还没有时间深入研究。可能让你开始正确的方向。祝你好运! – Jim 2012-08-13 18:11:31

+0

在这个巨大的呼叫链中,什么时候会出现错误?方法/属性调用链的问题在于,如果您知道链中的所有内容都存在并且会成功,那么它们只能安全使用。如果没有,你有这样的错误的危险。真的很吸引人的地方在于,没有干净的方法来强制PHP抛出一个可捕获的异常,但这有点偏离主题。我们可以看到XML源代码吗? – DaveRandom 2012-08-13 18:11:32

+0

@DaveRandom肯定给我一分钟 – 2012-08-13 18:12:50

回答

1

这里你的问题是,你正在测试太远链->运营商:

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

为了甚至测试这个,PHP mu ST评估每个->运营商又将

  1. $product->Product [OK]
  2. $product->Product->LowestOfferListings [OK]
  3. $product->Product->LowestOfferListings->LowestOfferListing [空集,如果没有价格]
  4. $product->Product->LowestOfferListings->LowestOfferListing->Price [未定义]
  5. 之后,你已经太迟了。

此外,SimpleXML对象不会评估为false,即使它是一个空列表,所以您确实需要使用isset。为了发现无代价的情况下,你因此也需要这样的:

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

[注:我没有实际测试上面,所以我可能出了问题在一些具体的,但我敢肯定这是你的基本问题。]

+0

谢谢,如果你看看我上面的评论,我已经决定使用'if(isset())'。 – 2012-08-21 02:00:16