1

I've building an XML editor and am having a little trouble parsing XML files that are passed in.

At the moment I can only parse a certain depth of XML elements, and I was wondering how you could get the deepest XML element, because at the moment I have a series of for each loops that loop through xmlElem.children() but this only allows me to parse to a certain depth, so it's rather limited.

Any help appreciated!

Cheers, Harry.

Harry Northover
  • 553
  • 1
  • 6
  • 24

3 Answers3

2

Sounds like a job for recursion http://en.wikipedia.org/wiki/Recursion_%28computer_science%29

This function will traverse the entire XML structure, regardless of its size/depth.

function parseChildren(parent:XML):void {
    for each(child:XML in parent.children()) {
        //do whaterver...

        if(child.children().length() > 0) {
             parseChildren(child);
        {
    }
}
Tyler Egeto
  • 5,505
  • 3
  • 21
  • 29
0

This may be what you're looking for:

var _xml:XML;
for each(var _element:XML in _xml.descendants())
{
   trace(_element);
}

While this loop is just an example, the "descendants()" function returns children, grandchildren, great-grandchildren, and so-on and so-forth, parsing your XML object and looking at all elements regardless of size and depth.

AuRise
  • 2,253
  • 19
  • 33
0

You can also use the following SimpleXML class converted to Flash's AS3 from Flex AS3. You can find it here: Author website or directly download it from here: SimpleXML source

I found this class very helpful in accessing XMLs.

RaamEE
  • 3,017
  • 4
  • 33
  • 53