在搜索亚马逊给我的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。如果找不到属性Amount将会是哪个。
金额不是属性的示例:
<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>
我不明白问题可能是什么。我提前感谢你的帮助。
答案 0 :(得分:2)
失败是因为您正在检查另一个不存在的节点后面的节点。
在一个例子中,节点&#34; ListPrice&#34;不存在,但您正在检查是否&#34; ListPrice-&gt;金额&#34; == 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;
}