Sometimes, you have to have an Array rather than XML. There is no straightforward conversion, though the XML class does deliver a lot of Array-like content.

This is a quick and incomplete conversion of XML to an array. The main caveat you need to know is that all information is assumed to be in attributes, and not element text. Also, it ignores the first child of the XML file, processing only the children. Since there are many ways to set up XML “correctly”, this was a solution for a specific problem, and may not fit your needs.

But look at it anyway! There are many useful processes and setup that you may glean from this, and adding element text to the array is not a huge leap:

// This function is recursive.
protected function x2a(xd:XML):Array
{
	var arr:Array = [];
	var idx:int = 0;
	
	// attributes for this child
	var val2:int = xd.attributes().length();
	for (var j:int = 0; j < val2; j++) 
	{
		xd.attributes()[j]
		arr[idx++] = xd.attributes()[j].toString();
	}
	
	// elements for this child
	var val:int = xd.elements().length();
	for (var i:int = 0; i < val; i++) 
	{
		arr[idx++] = x2a(xd.elements()[i]);
	}
	
	return arr;
}

– 30 –

Advertisements