How to recursively traverse an XML object in AS3
Published by Nicholas Dunbar on December 24th, 2012
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***** | |
* Function Name: | |
* dirXml | |
* Description: | |
* Print out an XML Tree to the console (trace) using a "Depth-First Tree Traversal" | |
* http://en.wikipedia.org/wiki/Tree_traversal#Depth-first | |
******/ | |
//How to Use this function (function source code below): | |
/* | |
//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> | |
*/ | |
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; | |
} | |
/***** | |
* Return a string of all the attributes in a node | |
******/ | |
private function _getAttributesStr(atribs:XMLList):String{ | |
var allAtribs:String = ""; | |
for each (var item_xml in at ribs){ | |
allAtribs += item_xml.name()+"=\""+item_xml+"\" "; | |
} | |
return allAtribs; | |
} | |
/***** | |
* Format XML string by converting it to XML and then back to a string | |
******/ | |
private function _htmlEscape(str:String):String | |
{ | |
return XML( new XMLNode( XMLNodeType.TEXT_NODE, str ) ).toXMLString(); | |
} | |
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](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.