Php SimpleXML检查SimpleXML对象是否存在
我使用if语句来检查XML对象是否存在,但由于我得到了Php SimpleXML检查SimpleXML对象是否存在,php,xml,simplexml,Php,Xml,Simplexml,我使用if语句来检查XML对象是否存在,但由于我得到了试图获取非对象错误的属性,因此它不能正常工作 下面是代码 foreach($resp->GetLowestOfferListingsForASINResult as $product){ $isbn = ""; $price = ""; $condition = ""; if($product->Product->Identifiers->MarketplaceASIN->ASIN){
试图获取非对象错误的属性,因此它不能正常工作
下面是代码
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返回结果(如果存在价格)
真的
A1F83G8C2ARO7P
000726965X
使用
造币厂
亚马逊
真的
0-2天
95-97%
1.
500720
英镑
12.65
英镑
12.65
英镑
0
假的
使用
可接受
商人
真的
0-2天
95-97%
1.
73208
英镑
15.41
英镑
12.61
英镑
2.80
假的
使用
造币厂
商人
真的
0-2天
98-100%
1.
2125
英镑
22.80
英镑
20
英镑
2.80
假的
使用
造币厂
商人
真的
0-2天
90-94%
1.
68301
英镑
23.14
英镑
20.34
英镑
2.80
假的
使用
造币厂
商人
假的
0-2天
90-94%
2.
4114
英镑
25.39
英镑
22.59
英镑
2.80
假的
使用
造币厂
商人
真的
0-2天
95-97%
1.
34661
英镑
29.20
英镑
26.40
英镑
2.80
假的
使用
非常好
商人
假的
0-2天
95-97%
1.
49504
英镑
33.16
英镑
30.36
英镑
2.80
假的
09a1deae-94ec-4661-95ae-49567ab00eb8
xml返回结果(如果价格不存在)
真的
A1F83G8C2ARO7P
0957143826
13918cb9-69e5-4445-ad24-70f6fda06f19
您的问题在于,您测试的操作员链太远了
if ( $product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount )
为了测试这一点,PHP必须依次评估每个->
操作符
$product->product
[确定]
$product->product->LowestOfferListings
[确定]
$product->product->LowestOfferListing->LowestOfferListing
[如果没有价格,则为空集]
$product->product->LowestOfferListing->LowestOfferListing->Price
[未定义]
在这之后,你已经太晚了
此外,SimpleXML对象即使是空列表,也不会计算为false
,因此您确实需要使用isset
。因此,为了发现无价格情况,您需要:
if ( isset($product->Product->LowestOfferListings->LowestOfferListing) )
[注意:我没有实际测试上述内容,因此我可能在某些特定方面出错,但我非常确定这是您的基本问题。]您的问题在于,您测试的操作员链太远了
if ( $product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount )
为了测试这一点,PHP必须依次评估每个->
操作符
$product->product
[确定]
$product->product->LowestOfferListings
[确定]
$product->product->LowestOfferListing->LowestOfferListing
[如果没有价格,则为空集]
$product->product->LowestOfferListing->LowestOfferListing->Price
[未定义]
在这之后,你已经太晚了
此外,SimpleXML对象即使是空列表,也不会计算为false
,因此您确实需要使用isset
。因此,为了发现无价格情况,您需要:
if ( isset($product->Product->LowestOfferListings->LowestOfferListing) )
[注意:我没有实际测试上述内容,因此我可能在某些特定方面出错,但我非常确定这是您的基本问题。]我也有相同/类似的问题,并且被告知我需要检查子节点是否存在。然而,我还没有时间深入研究这个问题。不过,这可能会让你朝着正确的方向开始。祝你好运在这个庞大的调用链中,错误是在什么时候出现的?方法/属性调用链的问题在于,只有当您知道链中的所有内容都存在并将成功时,才可以安全地使用它们。否则,您将面临这样的错误风险。真正糟糕的是,没有干净的方法强制PHP抛出一个可捕获的异常,但这有点离题。我们能看看XML源代码吗?@DaveRandom当然可以,给我一个minute@DaveRandomXML的问题是大部分数据都在那里,而