PDA

View Full Version : xml convert to array?


exclosion
09-03-2009, 06:30 AM
How to convert xml to array? here's my xml data

<revenuelist>
<list>
<month name="Dec-99" revenue="100">
<region name="Asia" revenue="100"/>
<region name="Emerging Markets" revenue="100"/>
<region name="Europe" revenue="100"/>
<region name="Latin America" revenue="100"/>
<region name="North America" revenue="100"/>
</month>
<month name="Jan-00" revenue="101.5207">
<region name="Asia" revenue="99.7953"/>
<region name="Emerging Markets" revenue="103.0175"/>
<region name="Europe" revenue="104.8088"/>
<region name="Latin America" revenue="102.5237"/>
<region name="North America" revenue="101.017"/>
</month>
<month name="Feb-00" revenue="107.1578">
<region name="Asia" revenue="102.4902"/>
<region name="Emerging Markets" revenue="106.7674"/>
<region name="Europe" revenue="115.551"/>
<region name="Latin America" revenue="104.131"/>
<region name="North America" revenue="108.0212"/>
</month>
</list>
</revenuelist>

wvxvw
09-03-2009, 06:52 AM
Mmmm... I see that this is connected to your other question, so, again, post an example of how you want the data to be formatted. This is because you cannot really convert XML to Array because XML is a tree-like structure.

exclosion
09-03-2009, 07:18 AM
into something like that: { Month: "Dec-99", AllRegions:100, Asia: 100, EmergingMarket:100, Europe:100, LatinAmerica:100, NorthAmerica:100},
{ Month: "Jan-00", AllRegions:101.5207, Asia: 99.7953, EmergingMarket:103.0175, Europe:104.8088, LatinAmerica:102.5237, NorthAmerica:101.017},
{ Month: "Feb-00", AllRegions:107.1578, Asia: 102.4902, EmergingMarket:106.7674, Europe:115.551, LatinAmerica:104.131, NorthAmerica:108.0212},
I didn't know that till you mention.
This is because you cannot really convert XML to Array because XML is a tree-like structure.
BTW can I know if the array is written inside .as file, is that consider as hard coding?

wvxvw
09-03-2009, 08:18 AM
var xml:XML =
<revenuelist>
<list>
<month name="Dec-99" revenue="100">
<region name="Asia" revenue="100"/>
<region name="Emerging Markets" revenue="100"/>
<region name="Europe" revenue="100"/>
<region name="Latin America" revenue="100"/>
<region name="North America" revenue="100"/>
</month>
<month name="Jan-00" revenue="101.5207">
<region name="Asia" revenue="99.7953"/>
<region name="Emerging Markets" revenue="103.0175"/>
<region name="Europe" revenue="104.8088"/>
<region name="Latin America" revenue="102.5237"/>
<region name="North America" revenue="101.017"/>
</month>
<month name="Feb-00" revenue="107.1578">
<region name="Asia" revenue="102.4902"/>
<region name="Emerging Markets" revenue="106.7674"/>
<region name="Europe" revenue="115.551"/>
<region name="Latin America" revenue="104.131"/>
<region name="North America" revenue="108.0212"/>
</month>
</list>
</revenuelist>;

var array:Array = [];
xml.list.month.(addToArray(valueOf(), array));
function addToArray(node:XML, where:Array):void
{
var o:Object = { month: node.@name.toString(),
allRegions: node.@revenue.toString() // what did you want to put here?
};
node.region.(o[@name.toString()] = @revenue);
array.push(o);
}

var primeTypes:Object =
{
"int" : "int",
"uint" : "uint",
"Number" : "Number",
"Boolean" : "Boolean",
"String" : "String",
"XML" : "XML",
"XMLList" : "XMLList",
"null" : "null",
"void" : "undefined",
"Date" : "Date",
"RegExp" : "RegExp"
}
var s:String = "";
function write(...rest):void
{
s += rest.join(" ");
}

function arrayDump(input:Array, nesting:int = 0):void
{
write("[");
var i:int;
for each (var o:Object in input)
{
i++;
if (o is Array)
{
arrayDump(o as Array);
}
else if (getQualifiedClassName(o) in primeTypes)
{
write(String(o) + ", ");
}
else
{
objectDump(o);
}
if (i < input.length) write(", ");
}
write("]");
}

function objectDump(input:Object, nesting:int = 0):void
{
var i:int;
write("{");
for (var p:String in input)
{
if (i) write(", ")
write(p, ":", input[p]);
i++;
}
write("}");
}

arrayDump(array);
trace(s);
/*
[{Europe : 100, Emerging Markets : 100, month : Dec-99, North America : 100, Latin America : 100, allRegions : 100, Asia : 100},
{Europe : 104.8088, Emerging Markets : 103.0175, month : Jan-00, North America : 101.017, Latin America : 102.5237, allRegions : 101.5207, Asia : 99.7953},
{Europe : 115.551, Emerging Markets : 106.7674, month : Feb-00, North America : 108.0212, Latin America : 104.131, allRegions : 107.1578, Asia : 102.4902}]
*/
And yes, if you write array in that manner in AS file it is called hardcoding.

exclosion
09-03-2009, 09:09 AM
wa..seem very complicated..
var xml:XML (the whole list of xml have to paste in the mxml?)
Also, I dun really understand what the array and object dump do. Do you mind giving me a brief explanation?

wvxvw
09-03-2009, 11:40 AM
var xml:XML (the whole list of xml have to paste in the mxml?)
-- no, I just put it there for test, I believe you'll be retrieving the XML from some remote source.
-- arrayDump and objectDump are just for convenience of tracing out the contents of an array... because outherwise it's difficult to show what exactly there is in the array.

Ultimately what you need is only this part:
var array:Array = [];
xml.list.month.(addToArray(valueOf(), array));
function addToArray(node:XML, where:Array):void
{
var o:Object = { month: node.@name.toString(),
allRegions: node.@revenue.toString() // what did you want to put here?
};
node.region.(o[@name.toString()] = @revenue);
array.push(o);
}
The rest is an explanation of what this part does...