1

私はこれをしばらく研究していて、私のコードに問題を見つけることができないようです。アイテムの価格を取得しようとしていますが、価格がある場合はこれでうまくいきますが、価格がない場合はエラーが発生します。
コードは次のとおりです。

    /* Amazon Offers ALGORITHM */
$parsed_xml = amazon_xml($isbn);

$current = $parsed_xml->ListMatchingProductsResult->Products->Product;
$asin = $current->Identifiers->MarketplaceASIN->ASIN;

// get information based on the items ASIN
$price_xml = amazonPrice_xml($asin);
    if($price_xml) {
    while(count($lowestPrices) < 2)
    {

        // check to see if there are values
        if(xml_child_exists($parsed_xml, $current->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount))
           { 
            $listPrice = $current->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount;
          } else {
            $listPrice = 0;
          }
        $currentPrice = $price_xml ->GetLowestOfferListingsForASINResult->Product->LowestOfferListings->LowestOfferListing;
        print_r($listPrice); 

子ノードをチェックする私の関数は次のとおりです。

    function xml_child_exists($xml, $childpath)
{
$result = $parsed_xml->xpath($childpath);
if (count($result)) {
    return true;
} else {
    return false;
}
}
4

2 に答える 2

1

property_exists()を使用します 。これを使用して、SimpleXMLを使用してxmlに新しい子を追加するときに重複する子をチェックします。それはあなたのために働くはずです。子の名前とその親を渡すだけの場合。

function xml_child_exists($xml, $child)
{
 return property_exists($xml, $child);
}
于 2012-07-18T20:16:48.757 に答える
1

これを試して、子ノードが存在するかどうかを確認してください

function xml_child_exists($xml, $childpath)
 {
  //$result = $parsed_xml->xpath($childpath); $parsed_xml variable?
    $result = $xml->xpath($childpath);
   if (isset($result)) { // changed from count to isset
    return TRUE;
   } else {
    return FALSE;
   }
}
于 2012-07-18T20:16:58.073 に答える