This is a method I came up with for taking a loaded simple XML document and turning it into a top level object in Flash. Basically it trawls through a XML tree and uses the attributes and content to build a object tree recognized by flash.

In AS3 this is the XMLDocument class. But obviously it places the first parent of the XML file at _root which doesn’t exist  in AS3!

The reasons why I did this was to simplify the retrieval of information from a XML object and to mash it into the actionscript setup that are used inside my SWiSH Templates. Effectively allowing me to add in a XML setup file. The SWiSH Template use a mock JSON setup inside a SWiSH Max file. Is it is a collecting of inline arguments, strings and arrays that define the content.

Now to the script:

function convertNodes (node){
// The process is a forward flow out and down the XML
// tree, and the finParentPath is used to keep track of the
// node branches it is on.
if(node.nodeName != null ){
var new_parent: String = node.nodeName.toLowerCase();
var parent_node: String = node.parentNode.nodeName.toLowerCase();
new_path1 =  findParentPath(node.parentNode.parentNode);//
if(new_path1 == "_root" && parent_node == "undefined"){
//
_root[new_parent] = new Object();
new_obj = _root[new_parent];
new_obj = node.childNodes;
}

if(new_path1 == "_root"){
new_obj = eval(new_path1)[parent_node];
new_obj[new_parent] = node.childNodes;
} else {
new_obj = eval(new_path1)[parent_node];
new_obj[new_parent] = node.childNodes;
}
}
for( var child = node.firstChild; child != null; child = child.nextSibling){
convertNodes(child);
}
}
function findParentPath (node2){
var new_path: String = "";
new_node = node2;
while (new_node.nodeName != null){
// from the node, we step back to its parent, test if that as a mc path exisits then step agains, forming the
// path back to _root which the above function will use to build the new object.
new_path += new_node.nodeName.toLowerCase() +".";
var new_node = new_node.parentNode;
};
var final_path1 : Array = new_path.split(".");
var final_path2: Array = final_path1.reverse();
var  final_path: String = "_root"+(final_path2.join("."));
return final_path;
}

To initiate the call just throw your well formed XML file at it like this:

convertNodes(siteSpecs_xml);

This is for actionscript 2 but I’ll script out one for AS3 shortly to use the XMLDocument class. The great thing about this method is that it is entirely flexible. I will consider adapting it to the other forms of XML that AS3 uses, I see issues with some of the XMLList needing distinct variable names, prehaps I could throw XMLList with the same attributes name into an array? There are a few translation issues as well, say what do to with a Namespace definition like <content:section>. Interesting ideas.