Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not finding elements using getElementsByTagName() using DOMDocument

Tags:

dom

php

xml

I'm trying to loop through multiple <LineItemInfo> products contained within a <LineItems> within XML I'm parsing to pull product Ids out and send emails and do other actions for each product.

The problem is that it's not returning anything. I've verified that the XML data is valid and it does contain the necessary components.

$itemListObject = $orderXML->getElementsByTagName('LineItemInfo');
var_dump($itemListObject->length);
var_dump($itemListObject);

The output of the var_dump is:

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

This is my first time messing with this and it's taken me a couple of hours but I can't figure it out. Any advice would be awesome.

EDIT:

My XML looks like this... except with a lot more tags than just ProductId

<LineItems>
     <LineItemInfo>
         <ProductId href='[URL_TO_PRODUCT_XML]'>149593</ProductId>
     </LineItemInfo>
     <LineItemInfo>
         <ProductId href='[URL_TO_PRODUCT_XML]'>149593</ProductId>
     </LineItemInfo>
</LineItems>

Executing the following code does NOT get me the ProductId

$itemListObject = $orderXML->getElementsByTagName('LineItemInfo');
foreach ($itemListObject as $element) { 
        $product = $element->getElementsByTagName('ProductId');
        $productId = $product->item(0)->nodeValue;
        echo $productId.'-';
}

EDIT #2

As a side note, calling

$element->item(0)->nodeValue

on $element instead of $product caused my script's execution to discontinue and not throwing any errors that were logged by the server. It's a pain to debug when you have to run a credit card to find out whether it's functioning or not.

like image 896
Webnet Avatar asked Jan 15 '10 17:01

Webnet


2 Answers

DOMDocument stuff can be tricky to get a handle on, because functions such as print_r() and var_dump() don't necessarily perform the same as they would on normal arrays and objects (see this comment in the manual).

You have to use various functions and properties of the document nodes to pull out the data. For instance, if you had the following XML:

<LineItemInfo attr1="hi">This is a line item.</LineItemInfo>

You could output various parts of that using:

$itemListObjects = $orderXML->getElementsByTagName('LineItemInfo');
foreach($itemListObjects as $node) {
    echo $node->nodeValue;    //echos "This is a line item."
    echo $node->attributes->getNamedItem('attr1')->nodeValue;  //echos "hi"
}

If you had a nested structure, you can follow basically the same procedure using the childNodes property. For example, if you had this:

<LineItemInfo attr1="hi">
  <LineItem>Line 1</LineItem>
  <LineItem>Line 2</LineItem>
</LineItemInfo>

You might do something like this:

$itemListObjects = $orderXML->getElementsByTagName('LineItemInfo');
foreach($itemListObjects as $node) {
    if ($node->hasChildNodes()) {
      foreach($node->childNodes as $c) {
         echo $c->nodeValue .",";
      }
    }
}

//you'll get output of "Line 1,Line 2,"

Hope that helps.

EDIT for specific code and XML

I ran the following code in a test script, and it seemed to work for me. Can you be more specific about what's not working? I used your code exactly, except for the first two lines that create the document. Are you using loadXML() over loadHTML()? Are there any 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 as $element) { 
    $product = $element->getElementsByTagName('ProductId');
    $productId = $product->item(0)->nodeValue;
    echo $productId.'-';
}

//outputs "149593-149595-"
like image 61
zombat Avatar answered Oct 19 '22 23:10

zombat


XML tags tend to be lower-camel-case (or just "camel-case"), i.e. "lineItemInfo", instead of "LineItemInfo" and XML is case-sensitive, so check for that.

like image 34
Eric Mickelsen Avatar answered Oct 20 '22 00:10

Eric Mickelsen