I have a XML which goes like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Row>
<ColumnA>2013-08-07</ColumnA>
<ColumnB>Pizza</ColumnB>
</Row>
<Row>
<ColumnA>2013-04-07</ColumnA>
<ColumnB>Burger</ColumnB>
</Row>
<Row>
<ColumnA>2013-04-07</ColumnA>
<ColumnB>Pizza</ColumnB>
</Row>
The XML goes on like this for hundreds of Rows. What I am trying to achieve is this. "Output: Pizza: 2 occurances on 2013-08-07 & 2013-04-07." So far this is what I've tried:
//Variables
var myXML:XML;
var xmlLoader:URLLoader = new URLLoader();
//Check that the XML is loaded
xmlLoader.load(new URLRequest ("myXML.xml"));
xmlLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
//Create a XMLList for Column A data
var items:XMLList = myXML.Row.ColumnA.text()
//Create a XMLList for Rows
var rows:XMLList = myXML.Row
//Create a XMLList for Column B data (Dates)
var dates:XMLList = myXML.Row.ColumnB.text()
//Create an empty object to fill in the occurances of Column A
var columnA:String;
var map:Object = {};
for each (columnA in items)
{
if(!map[columnA])
map[columnA] = 1;
else
map[columnA]++;
}
for ( columnA in map )
{
trace(columnA, "=", map[columnA], "times. On dates ");
}
}
My newbie attempt at trying to see if I can relate column B to column A
var columnB:String;
for ( var i:uint=0; i<rows.length(); i++ )
{
if ( columnA[i] == myXML.Row.ColumnA[i] )
{
trace( columnA[i], "=", columnB[i] )
}
}
It's puzzling me. Currently in output panel I get...
Pizza 2 times. On Dates
Burger 1 times. On Dates
etc.
Is there a way to include the dates in the trace output? So I get something like:
Pizza 2 times. On Dates 2013-08-07, 2013-04-07
I wouldn't extract anything from the XML before parsing it:
var map:Object = {};
var meal:String;
//loop through each row
for each (var node:XML in myXML.Row)
{
meal = node.ColumnB.text();
//if the meal does not exist as a key in our map yet, create it and put an empty array in it
if (!map[meal])
map[meal] = [];
//fill the array of dates with the current date
map[meal].push(node.ColumnA.text());
}
There you get an object filled like that:
{Pizza: ["2013-08-07", "2013-04-07"], Burger: ["2013-04-07"]}
You just have to go through it:
for (var meal:String in map)
{
trace(meal, "found", map[meal].length, "times on", map[meal]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.