Traversing XML structures in AS3

There may come a time when one has to traverse some XML data….

Alright, so maybe never. But let’s just pretend that the ability to sort XML data might be of some use to someone out there one day! After working on a project this week, this is what I figured out about traversing XML structures.

// load XML file into Flash
var myNewXML:XML;
var myNewXMLLength:Number;
var myNewLoader:URLLoader = new URLLoader();
myNewLoader.load(new URLRequest("xml/singapore.xml"));
myNewLoader.addEventListener(Event.COMPLETE, processmyNewXML);
function processmyNewXML(e:Event):void{
 myNewXML = new XML(e.target.data);
 myNewXMLLength = myNewXML.*.length();
 //trace(myNewXML); // traces the ENTIRE xml sheet
 //trace(myNewXMLLength); // traces the number of items in xml sheet
 //trace(myNewXML.row[0]); // traces the first row
 //trace(myNewXML.row[1]); // traces the second row
 //trace(myNewXML.row[0].name); // traces the first row's name field 
}

// creating a new XMLList to sort and loop through specific category filters
var myNewXMLList:XMLList = myNewXML.row.(category == “Ind” && type == “Pte”);
for (var i:int = 0; i < myNewXMLList.length(); i++)
{
var myNewXMLElement:XML = myNewXMLList[i];
//trace(myNewXMLElement) // traces xml list of only the filtered items
//trace(i + “: ” + myNewXMLElement.name); // trace the number and name
}

// checking for a value
if (“myValue” in myNewXMLList) {
trace(“myValue exists in my XMLList”);
}

/*
Other useful operators to use to traverse XML:
.parent();
.childIndex();
.length();
.toString();
.firstChild();
.nextChild();

You can also use a combination in a chain to find specific fields
*/

Q: Should you put the XML data into an Array before sorting it?

A: There isn’t a real need to put XML into an Array for handling in AS3. Apparently ECMAScript (which includes Actionscript, Javascript, and JScript) now has native XML support (E4X), allowing XML to be treated as its various primitive parts, rather than a seperate object on its own.

I also wondered if I should put it in an array and sort it, as I initially didnt know how to sort for combinations of categories on different “levels”. But in the end, it seems that it is not necessary to put your values into an array because there are certainly ways to sort and loop it through in the form of an XMLList. You could potentially create different XMLLists from the same XML file that has been loaded.

Q: How does one export Excel to XML on Microsoft Excel for Mac?

A: WELL, YOU CAN’T. It seems as if every single version of Microsoft Excel for Mac (including Excel 2011) is tragically unable to export XLS files into XML directly. It is said online that Windows Excel has both the Visual Basic editor and the magical XML function that Mac Excel is inexplicably bereft of. Excel 2007 for example, has no macros or VB editor at all, rendering it completely pointless. Why was such a simple and basic function omitted from all the Mac versions of Excel? As for alternatives like OpenOffice and NeoOffice, let’s not even go there since they always crash on my Mac.

So my final solution? I saved all my XLS files to CSV (comma-seperated values), and then I used an opensource online tool called Mr Data Convertor to convert it to XML. OPENSOURCE WIN.

Q: How do I install an open source Syntax Highlighter?

A: You only need to add a little snippet to get a syntax highlighter running on your page. For the benefit of this post I also installed Alex Gorbatchev’s syntax highlighter, an open source code syntax highlighter which can autoformat nearly any common programming language in blogger or your own site. If you are in the habit of posting code then this highlighter and HTML encoder may come in handy for converting all your pointy brackets into their HTML tags.