php - Not finding elements using getElementsByTagName() using DomDocument -


i'm trying loop through multiple <lineiteminfo> products contained within <lineitems> within xml i'm parsing pull product ids out , send emails , other actions each product.

the problem it's not returning anything. i've verified xml data valid , contain necessary components.

$itemlistobject = $orderxml->getelementsbytagname('lineiteminfo'); var_dump($itemlistobject->length); var_dump($itemlistobject); 

the output of var_dump is:

int(0) object(domnodelist)#22 (0) { } 

this first time messing , it's taken me couple of hours can't figure out. advice awesome.

edit:

my xml looks this... except lot more tags productid

<lineitems>      <lineiteminfo>          <productid href='[url_to_product_xml]'>149593</productid>      </lineiteminfo>      <lineiteminfo>          <productid href='[url_to_product_xml]'>149593</productid>      </lineiteminfo> </lineitems> 

executing following code not me productid

$itemlistobject = $orderxml->getelementsbytagname('lineiteminfo'); foreach ($itemlistobject $element) {          $product = $element->getelementsbytagname('productid');         $productid = $product->item(0)->nodevalue;         echo $productid.'-'; } 

edit #2

as side note, calling

$element->item(0)->nodevalue 

on $element instead of $product caused script's execution discontinue , not throwing errors logged server. it's pain debug when have run credit card find out whether it's functioning or not.

domdocument stuff can tricky handle on, because functions such print_r() , var_dump() don't perform same on normal arrays , objects (see this comment in manual).

you have use various functions , properties of document nodes pull out data. instance, if had following xml:

<lineiteminfo attr1="hi">this line item.</lineiteminfo> 

you output various parts of using:

$itemlistobjects = $orderxml->getelementsbytagname('lineiteminfo'); foreach($itemlistobjects $node) {     echo $node->nodevalue;    //echos "this line item."     echo $node->attributes->getnameditem('attr1')->nodevalue;  //echos "hi" } 

if had nested structure, can follow same procedure using childnodes property. example, if had this:

<lineiteminfo attr1="hi">   <lineitem>line 1</lineitem>   <lineitem>line 2</lineitem> </lineiteminfo> 

you might this:

$itemlistobjects = $orderxml->getelementsbytagname('lineiteminfo'); foreach($itemlistobjects $node) {     if ($node->haschildnodes()) {       foreach($node->childnodes $c) {          echo $c->nodevalue .",";       }     } }  //you'll output of "line 1,line 2," 

hope helps.

edit specific code , xml

i ran following code in test script, , seemed work me. can more specific what's not working? used code exactly, except first 2 lines create document. using loadxml() on loadhtml()? there errors?

$orderxml = new domdocument(); $orderxml->loadxml(" <lineitems>      <lineiteminfo>          <productid href='[url_to_product_xml]'>149593</productid>      </lineiteminfo>      <lineiteminfo>          <productid href='[url_to_product_xml]'>149593</productid>      </lineiteminfo> </lineitems> ");  $itemlistobject = $orderxml->getelementsbytagname('lineiteminfo'); foreach ($itemlistobject $element) {      $product = $element->getelementsbytagname('productid');     $productid = $product->item(0)->nodevalue;     echo $productid.'-'; }  //outputs "149593-149595-" 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -