Loop XML including all its children using xpath and SimpleXMLElement

Posted by Andersson on Stack Overflow See other posts from Stack Overflow or by Andersson
Published on 2010-04-02T00:12:58Z Indexed on 2010/04/02 3:43 UTC
Read the original article Hit count: 327

Filed under:
|
|

Im having a big xml tree with a simple structure. All I want to do is get a node by its attribute using xpath and loop the children.

param1 - correct language

param1 & param3 - correct node by its id and the parents id. (Need to use parent id when multiplie id appears. The id is not declared as id in DTD)

    public function getSubmenuNodesRe($language, $id1,$id2) {
    $result = $this->xml->xpath("//*[@language='$language']//*[@id='$id1']//menuitem[@id='$id2']/*");
    return $result;
}

This works well, I get the node Im expecting. I having no problems looping the array returned by the xpath query. But when i try to loop childnodes all attributes are gone and many of the methods also. Except getName().

    function printNodeName($node) 
{
    echo $node->getName(). " - \"" . $node['id']. "\"<br>";

    if($node->children()->getName() == "menuitem")
    {
        $s = $node->children();
        printNodeName($s);
    }
}



$result = $xmlNode->getSubmenuNodesRe($_GET['language'], 'foo','bar');
if ($result) {
    while(list( , $node) = each($result)) {
        if ($node->getName() == "menuitem") { 
            printNodeName($node);
        }
    }
}

Why is that? only the name will be printed for the $s.

Really need help to solve this one, any assistance is deeply appreciated!

Thanks for your time.

© Stack Overflow or respective owner

Related posts about php

Related posts about xpath