1

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

1 Answer 1

4

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]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thankyou Kodiak! I am a beginner and I would be very delighted if you could add comments explaining each line of code.
It traces "Pizza found 1 times on 2013-08-07 Pizza found 1 times on 2013-04-07 Burger found 1 times on 2013-04-07" How can I make it count total occurances in all dates. For example: "Pizza: 2 occurances. Dates: 2013-08-07, 2013-04-07"
I copied exactly the code above and got Burger found 1 times on 2013-04-07 Pizza found 2 times on 2013-08-07,2013-04-07
Thankyou! :) also for including comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.