Search code examples
phpxmldomsimplexml

Php SimpleXML find specific child node in any level in the parent


I'm using SimpleXML to parse my xml file. I'm looping through it and in each node I need to get value of one specific tag. Here is an example

<node>
    <child1></child1>
    <findme></findme>
    <child2></child2>
</node>
<node>
    <child1>
        <findme></findme>
    </child1>
    <child2></child2>
</node>
<node>
    <child1></child1>
    <child2>
      <another>
            <findme></findme>
      </another>
    </child2>
</node>

In each node I need to get findme tag. But I don't know in which level it can be, all I know is a tagname


Solution

  • The only decision I came up with was to use this recursive function

    foreach($xml as $prod){
      ...
      $findme = getNode($prod, 'fabric');
      ...
    }
    
    function getNode($obj, $node) {
        if($obj->getName() == $node) { 
            return $obj;
        }
        foreach ($obj->children() as $child) {
            $findme = getNode($child, $node);
            if($findme) return $findme;
        }
    }
    

    Updated

    Also as was suggested in comments we can use DOMDocument class like this:

     $dom = new DOMDocument();
     $dom->LoadXML($xmlStr);
     $nodes = $dom->getElementsByTagName('node');
    
     foreach($nodes as $node)
     { 
        $findme = $node->getElementsByTagName("findme")->item(0);
        echo $findme->textContent."\r";
     }