du13520157325 2010-01-15 17:57
浏览 38
已采纳

使用DomDocument不使用getElementsByTagName()查找元素

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.

  • 写回答

2条回答 默认 最新

  • drouie2014 2010-01-15 18:29
    关注

    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-"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题