Last active
September 4, 2015 04:12
-
-
Save nicholasdunbar/8795580 to your computer and use it in GitHub Desktop.
A Depth-First Recursive Tree Traversal of an XML Tree in ActionScript 3 (AS3)
This file contains hidden or 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(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to change the font and font size in tree ?