Jump to content

Looping Through A SimpleXMLElement To Find A Node


JustinK101

Recommended Posts

I am trying to loop through a multi-leveled XML structure looking for a particular node. Here is what I have:

 

foreach($xml->children() as $child) {
   if($child->getName() == "subsuba") {
      return $child;
      break;
   }
}

 

This works fine is the XML strucuture is flat, for example like:

 

<a>value a</a>
<b>value b</b>
<c>value c</c>

 

But I have a multi-leveled XML strucuture like:

 

<a>
  <suba>
    <subsuba>value a</subsuba>
   </suba>
</a>
<b>
   <subb>value b</subb>
</b>
<c>value c</c>

 

So what happens is the first loop through children() is acutally another SimpleXMLObject and it just stops. How do I make this work?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.