Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 09-03-2009, 06:30 AM   #1
exclosion
Member
 
Join Date: Jul 2009
Posts: 53
Default xml convert to array?

How to convert xml to array? here's my xml data
Code:
<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>
exclosion is offline   Reply With Quote
Old 09-03-2009, 06:52 AM   #2
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

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.
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it
wvxvw is offline   Reply With Quote
Old 09-03-2009, 07:18 AM   #3
exclosion
Member
 
Join Date: Jul 2009
Posts: 53
Default

into something like that:
Code:
{ 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.
Quote:
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?
exclosion is offline   Reply With Quote
Old 09-03-2009, 08:18 AM   #4
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

ActionScript Code:
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.
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it
wvxvw is offline   Reply With Quote
Old 09-03-2009, 09:09 AM   #5
exclosion
Member
 
Join Date: Jul 2009
Posts: 53
Default

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?
exclosion is offline   Reply With Quote
Old 09-03-2009, 11:40 AM   #6
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

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:
ActionScript Code:
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...
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it
wvxvw is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:11 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.