php - Getting item inside an XML tag and call to children() on null error -


i working amazon api , have noticed there problem when item not exist in xml searching through. xml in response looks this:

<getmatchingproductforidresult status="success" idtype="upc" id="082686052344">   <products>     <product>       <identifiers>          <marketplaceasin>             <marketplaceid>atvpdkikx0der</marketplaceid>             <asin>b000idc1wo</asin>          </marketplaceasin>       </identifiers>       <attributesets>           <ns2:itemattributes xml:lang="en-us" xmlns:ns2="http://mws.amazonservices.com/schema/products/2011-10-01/default.xsd">           <ns2:binding>toy</ns2:binding>           <ns2:brand>rubie's</ns2:brand>           <ns2:color>black</ns2:color>           <ns2:department>unisex-child</ns2:department>           <ns2:feature>rubie's costume children's zorro hat , eye mask set</ns2:feature>           <ns2:feature>child's costume accessory</ns2:feature>           <ns2:feature>black eye mask</ns2:feature>           <ns2:itemdimensions>                <ns2:height units="inches">8.00</ns2:height>                <ns2:length units="inches">14.00</ns2:length>                <ns2:width units="inches">8.00</ns2:width>           </ns2:itemdimensions>           <ns2:label>rubies - domestic</ns2:label>           <ns2:languages>               <ns2:language>                   <ns2:name>english</ns2:name>                   <ns2:type>unknown</ns2:type>               </ns2:language>           </ns2:languages>           <ns2:listprice>                <ns2:amount>16.99</ns2:amount>                <ns2:currencycode>usd</ns2:currencycode>           </ns2:listprice>           <ns2:manufacturer>rubies - domestic</ns2:manufacturer>           <ns2:manufacturermaximumage units="months">180.0</ns2:manufacturermaximumage>           <ns2:manufacturerminimumage units="months">48.0</ns2:manufacturerminimumage>           <ns2:model>f5234_ns</ns2:model>           <ns2:packagedimensions>               <ns2:height units="inches">0.90</ns2:height>               <ns2:length units="inches">14.50</ns2:length>               <ns2:width units="inches">13.20</ns2:width>               <ns2:weight units="pounds">0.20</ns2:weight>           </ns2:packagedimensions>           <ns2:packagequantity>1</ns2:packagequantity>           <ns2:partnumber>f5234_ns</ns2:partnumber>           <ns2:productgroup>toy</ns2:productgroup>           <ns2:producttypename>toys_and_games</ns2:producttypename>           <ns2:publisher>rubies - domestic</ns2:publisher>           <ns2:smallimage>                <ns2:url>http://ecx.images-amazon.com/images/i/51zrwleh85l._sl75_.jpg</ns2:url>                <ns2:height units="pixels">75</ns2:height>                <ns2:width units="pixels">57</ns2:width>           </ns2:smallimage>           <ns2:studio>rubies - domestic</ns2:studio>           <ns2:title>rubie's costume children's zorro hat , eye mask set</ns2:title>           <ns2:warranty>no warranty</ns2:warranty>           </ns2:itemattributes>        </attributesets>     <relationships/>     <salesrankings>       <salesrank>         <productcategoryid>toy_display_on_website</productcategoryid>         <rank>481818</rank>       </salesrank>       <salesrank>        <productcategoryid>2229578011</productcategoryid>        <rank>469</rank>        </salesrank>        </salesrankings>    </product>   </products> </getmatchingproductforidresult> 

if there client error, receive this:

<getmatchingproductforidresult id="082686035408" idtype="upc" status="clienterror">    <error>      <type>sender</type>      <code>invalidparametervalue</code>      <message>invalid upc identifier 082686035408 marketplace atvpdkikx0der</message>    </error> </getmatchingproductforidresult> 

i totally unaware of how deal since doing mass scans @ time. far php code, have this:

$xmlfiles = glob("xml/*xml"); //checking if multiple xml files exist.  if(is_array($xmlfiles)){    foreach($xmlfiles $xmlfile){      $xml = simplexml_load_file($xmlfile);      //problem 'status' = 'client error'      foreach($xml->getmatchingproductforidresult $items) {          print_r($items);         //getting upc         if(isset($items['id'])){              $id = $items['id'];          }else{             $id = 'no id found';         }         //getting asin xml         if(isset($items->products->product->identifiers->marketplaceasin->asin)){              $asin = $items->products->product->identifiers->marketplaceasin->asin;         }else{             $asin = 'no asin found';         }         if(isset($items->products->product->salesrankings->salesrank[0]->rank)){              $salesrank = $items->products->product->salesrankings->salesrank[0]->rank;         }else{             $salesrank = 'sales rank not found';         }         if(($items['status']) != 'clienterror'){             if(isset($items->products->product->attributesets->children('ns2', true)->itemattributes->listprice->amount)) {                  $amount = $items->products->product->attributesets->children('ns2', true)->itemattributes->listprice->amount;             }else{             $amount = '0.00';             }             if(isset($items->products->product->attributesets->children('ns2', true)->itemattributes->packagedimensions->height) !== false){                 $height =$items->products->product->attributesets->children('ns2', true)->itemattributes->packagedimensions->height;                         } else {                 $height = '';             }             if(isset($items->products->product->attributesets->children('ns2', true)->itemattributes->packagedimensions->length) !== false){                 $length = $items->products->product->attributesets->children('ns2', true)->itemattributes->packagedimensions->length;             } else {                 $length = '';             }                    if(isset($items->products->product->attributesets->children('ns2', true)->itemattributes->packagedimensions->width)){                 $width = $items->products->product->attributesets->children('ns2', true)->itemattributes->packagedimensions->width;             } else {                 $width = '';             }              if(($length+$width+$height) < 27.75){                 $oversize = '';             } elseif (($length+$width+$height) > 27.75 && ($length+$width+$height) < 40) {                 $oversize = 'small oversize';             } elseif(($length+$width+$height) > 40){             $oversize = 'large oversize';             }              $sqlimport = "insert " . $filename . "(id, asin, salesrank, amount, oversize)             values('$id', '$asin', '$salesrank', '$amount', '$oversize')";              if ($sqlconnection->query($sqlimport) === true) {               echo "new record created successfully";              } else {               echo "error: " . $sqlimport . "<br>" .    $sqlconnection->error;         }     } else {         die('sorry unexpected error');     } } 

} } not sure how implement answer following code. sorting through multiple xml files have 5 of each tag in it.

i have error when there client error:

fatal error: call member function children() on null in j:\xampp\htdocs\phillip\src\marketplacewebserviceproducts\samples\csv_prep.php on line 110 

line 110 is:

if(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';     } 

the current file testing on have 80 xml files goes through.if has anyway me out, appreciate it. in advance.

rather using simplexml following uses standard domdocument , approach access nodes in xml document. once have found root node can test fr first child different if there error ( products if ok, error if not ok ) - point can fork processing if / else

i'm sure able convert or adopt approach using simplexml - never used don't know involved.

given drastic change in question's content when posted initial comment code shown above can see no need use xpath query - hence approach.

$dom = new domdocument(); $dom->loadxml( $strxml ); $dom->preservewhitespace = true;  $root=$dom->getelementsbytagname('getmatchingproductforidresult')->item(0); $children=$root->childnodes; $child=$children->item(1);  if( $child->tagname=='error' ){     echo 'oh no, broke interwebs'; } else {     echo 'all good, proceed process xml'; }  $dom = $root = null; 

full example:

$strxml='<getmatchingproductforidresult status="success" idtype="upc" id="082686052344">           <products>             <product>               <identifiers>                  <marketplaceasin>                     <marketplaceid>atvpdkikx0der</marketplaceid>                     <asin>b000idc1wo</asin>                  </marketplaceasin>               </identifiers>               <attributesets>                   <ns2:itemattributes xml:lang="en-us" xmlns:ns2="http://mws.amazonservices.com/schema/products/2011-10-01/default.xsd">                   <ns2:binding>toy</ns2:binding>                   <ns2:brand>rubie\'s</ns2:brand>                   <ns2:color>black</ns2:color>                   <ns2:department>unisex-child</ns2:department>                   <ns2:feature>rubie\'s costume children\'s zorro hat , eye mask set</ns2:feature>                   <ns2:feature>child\'s costume accessory</ns2:feature>                   <ns2:feature>black eye mask</ns2:feature>                   <ns2:itemdimensions>                        <ns2:height units="inches">8.00</ns2:height>                        <ns2:length units="inches">14.00</ns2:length>                        <ns2:width units="inches">8.00</ns2:width>                   </ns2:itemdimensions>                   <ns2:label>rubies - domestic</ns2:label>                   <ns2:languages>                       <ns2:language>                           <ns2:name>english</ns2:name>                           <ns2:type>unknown</ns2:type>                       </ns2:language>                   </ns2:languages>                   <ns2:listprice>                        <ns2:amount>16.99</ns2:amount>                        <ns2:currencycode>usd</ns2:currencycode>                   </ns2:listprice>                   <ns2:manufacturer>rubies - domestic</ns2:manufacturer>                   <ns2:manufacturermaximumage units="months">180.0</ns2:manufacturermaximumage>                   <ns2:manufacturerminimumage units="months">48.0</ns2:manufacturerminimumage>                   <ns2:model>f5234_ns</ns2:model>                   <ns2:packagedimensions>                       <ns2:height units="inches">0.90</ns2:height>                       <ns2:length units="inches">14.50</ns2:length>                       <ns2:width units="inches">13.20</ns2:width>                       <ns2:weight units="pounds">0.20</ns2:weight>                   </ns2:packagedimensions>                   <ns2:packagequantity>1</ns2:packagequantity>                   <ns2:partnumber>f5234_ns</ns2:partnumber>                   <ns2:productgroup>toy</ns2:productgroup>                   <ns2:producttypename>toys_and_games</ns2:producttypename>                   <ns2:publisher>rubies - domestic</ns2:publisher>                   <ns2:smallimage>                        <ns2:url>http://ecx.images-amazon.com/images/i/51zrwleh85l._sl75_.jpg</ns2:url>                        <ns2:height units="pixels">75</ns2:height>                        <ns2:width units="pixels">57</ns2:width>                   </ns2:smallimage>                   <ns2:studio>rubies - domestic</ns2:studio>                   <ns2:title>rubie\'s costume children\'s zorro hat , eye mask set</ns2:title>                   <ns2:warranty>no warranty</ns2:warranty>                   </ns2:itemattributes>                </attributesets>             <relationships/>             <salesrankings>               <salesrank>                 <productcategoryid>toy_display_on_website</productcategoryid>                 <rank>481818</rank>               </salesrank>               <salesrank>                <productcategoryid>2229578011</productcategoryid>                <rank>469</rank>                </salesrank>                </salesrankings>            </product>           </products>         </getmatchingproductforidresult>';   $dom = new domdocument(); $dom->loadxml( $strxml ); $dom->preservewhitespace = true;  $root=$dom->getelementsbytagname('getmatchingproductforidresult')->item(0); $children=$root->childnodes; $child=$children->item(1);  if( $child->tagname=='error' ){      echo 'oh no, broke interwebs';  } else {      if( !defined('br') ) define('br','<br />');      /* create xpath object */     $xp=new domxpath( $dom );      /* define prefix , namespace uri */     $namespace = 'http://mws.amazonservices.com/schema/products/2011-10-01/default.xsd';     $prefix = 'ns2';      /* associate namespace dom content */     $xp->registernamespace( $prefix, $namespace );        /* target particular node */     $nodename='manufacturer';      /* ex#1 run xpath query */     $col=$xp->query( "//{$prefix}:{$nodename}" );     if( $col ) echo $col->item(0)->nodevalue;          /* ex#2 */     $nodename='title';     $col=$xp->query( "//{$prefix}:{$nodename}" );     if( $col ) echo $col->item(0)->nodevalue;      /* alternatively, childnodes of particular node have namespaces */     $col=$dom->getelementsbytagnamens( $namespace, 'itemattributes')->item(0);     if( $col ){         foreach( $col->childnodes $child ) if( $child->nodetype==xml_element_node ) echo 'tag:'.$child->tagname.' -> value:'.$child->nodevalue . br;      } } $dom = $root = $xp = $col = $namespace = $prefix = $nodename = null; 

update

$col=$xp->query( "//marketplaceasin" );/* no namespace */ $asin=$col ? $col->item(0)->nodevalue : false;  $col=$xp->query('//salesrank/rank');/* no namespace */ $rank=$col ? $col->item(0)->nodevalue : false;  $col=$xp->query("//{$prefix}:listprice/{$prefix}:amount"); $amount=$col ? $col->item(0)->nodevalue : false;  $col=$xp->query("//{$prefix}:packagedimensions/{$prefix}:length"); $length=$col ? $col->item(0)->nodevalue : false;  $col=$xp->query("//{$prefix}:packagedimensions/{$prefix}:height"); $height=$col ? $col->item(0)->nodevalue : false;  $col=$xp->query("//{$prefix}:packagedimensions/{$prefix}:width"); $width=$col ? $col->item(0)->nodevalue : false;   echo $asin.br.$rank.br.$amount.br.$length.br.$height.br.$width.br.br.$root->tagname.br.$root->getattribute('id'); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -