How To Recursively Traverse an XML Object in AS3

I found lots of examples on how to recusively parse through an XML object using E4X in ActionScript 3 (AS3). None of these examples were very simple or to the point. Most relied on the specifics of the XML instead of making general code that could access any XML object. E4X has eliminated much of the need to approach XML in this fashion, but there are still reasons to go through XML like this. Thus I am providing this simple example below which will print out the XML object:

    public function dirXml(_xml:*, depth:int = 0):void{
            var returnStr:String;
            var xmlList:XMLList = _xml.children();
           
            if (xmlList.length() > 0) {
                trace( _htmlEscape("<"+_xml.name()+" "+_getAttributesStr(_xml.attributes())+">") );
                for each (var item_xml in xmlList){
                    dirXml(item_xml,depth+1);
                }
                trace( _htmlEscape("</"+_xml.name()+">") );
            } else {
                //this is a leaf
              trace( _htmlEscape(_xml));
            }
            return;
        }
        private function _getAttributesStr(atribs:XMLList):String{
            var allAtribs:String = "";
            for each (var item_xml in atribs){
                allAtribs += item_xml.name()+"=\""+item_xml+"\" ";
            }
            return allAtribs;
        }
        private function _htmlEscape(str:String):String
        {
            return XML( new XMLNode( XMLNodeType.TEXT_NODE, str ) ).toXMLString();
        }

Example:

var testXml = <l><a atribute1="sadsdaf" atribute2="asdfsda">
                 <b>
                   <g>test</g>
                 </b>
                 <c>asdfdsaf</c>
              </a></l>;
dirXml(testXml);

Output:

<l>

<a atribute1="sadsdaf" atribute2="asdfsda">

<b>

<g>

test

</g>

</b>

<c>

asdfdsaf

</c>

</a>

</l>

You can use the parameter depth to indent the data if you want but that is not the point of this example. If you wanted to have a pretty string that represents the data you could use http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/XML.html#prettyPrinting 

The point of this function is to give you a skeleton to work with in its most simple form so then you can easily customize it to your purposes, whatever they may be. For example if you are populating a tree or directory ui from XML this would come in handy. If the article needs clairfication or has an error please leave a comment.

Comments (1)add comment

steven loe said:

This is a nice solution that works as advertised. Only issue I ran into while using it with an xml file loaded at runtime. If you're using a loader to load your data, make sure you're passing an XML to this function by casting it to XML before passing it to dirXml. For some reason the function has the data type of its first param set to *. You won't get compile-time type checking with this set to *. --> function dirXml(_xml:*, depth:int = 0)
 
report abuse
vote down
vote up
June 08, 2010 | url
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

security image
Write the displayed characters


busy