Mantis - Quercus
Viewing Issue Advanced Details
3064 major always 11-11-08 04:17 11-11-08 04:17
alexbu  
 
normal  
new 3.2.1  
open  
none    
none  
0003064: DOMNamedNodeMap cannot be used in foreach statement
It's not possible to use 'foreach' statement to iterate over an instance of DOMNamedNodeMap, returnted by $domElement->attributes, where $domElement is DOMElement

Though, there is no problem with iterating over DOMNodeList, returned by $domElement->childNodes
<?php

$test_str = "<?xml version='1.0' encoding='utf-8'?>
<root>
    <first attr1='value1' attrx='attrx'/>
    <second attr2='value2'/>
    <third/>
</root>";

function node_value($node) {

    foreach ($node->childNodes as $item) {
        if ($item instanceof DOMText)
            continue;
        
        echo 'node: ' . $item->nodeName . "\n";
        
        if ($item->hasAttributes()) {
            foreach($item->attributes as $attr) {
                echo 'attribute: ' . $attr->nodeName . ' with value: ' . $attr->nodeValue . "\n";
            }
        }
        
        if ($item->hasChildNodes())
            node_value($item);
    }
    
}

$doc=new DOMDocument();
if($doc->loadXML($test_str)===false)
    return false;

echo 'root:' . $doc->documentElement->nodeName . "\n";

node_value($doc->documentElement);

There are no notes attached to this issue.