We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Working with XML data
Page Index Toggle Pages: 1
Working with XML data (Read 428 times)
Working with XML data
Oct 20th, 2009, 9:23pm
 
Hey guys, first time post. I'm relatively new with Processing so excuse me if I make stupid mistakes Smiley

I've imported data from my iTunes xml data file. I only needed the genre of each song, plus the amount of times I've played that song. So far I'm able to print the information correctly in the console, but I'm a bit stuck for the next phase.

I want to collect the amount of times a song has been played for each genre. For example, if I have three songs that have the genre of "Classic Rock" and have a playcount of 5, 3, and 1 respectively, how can I get Processing to recognize a specific genre and then add up data only relating to that specific genre? I'd assume everything would be stored in an integer variable, but I'm stuck.

Here's my code so far:

XMLElement xml;

void setup() {
 size(200, 200);
 xml = new XMLElement(this, "iTunesMusicLibrary.xml");
 XMLElement[] songData = xml.getChildren("dict/dict/dict");
 for (int i=0; i < songData.length; i++) {
   for (int j=0; j < songData[i].getChildCount(); j+=2 ) {
     XMLElement key = songData[i].getChild(j);
     XMLElement value = songData[i].getChild(j+1);
       if (key.getContent().equals("Genre") ) {
       println("Genre: " + value.getContent() );
       }
       if (key.getContent().equals("Play Count") ) {
       println("Play Count: " + value.getContent() );
       }
     }
   }
}
Re: Working with XML data
Reply #1 - Oct 20th, 2009, 11:51pm
 
If I understood correctly, you need to associate a number to a string, right
A way to do that is to use a HashMap.
Classical method: get() the genre from the hash map. If null, not stored yet, put() it. Otherwise, you have the previous count, update it and put() it.
Re: Working with XML data
Reply #2 - Oct 21st, 2009, 10:18am
 
Okay I think that will do it. Yeah I need Processing to recognize a centain genre, then add up all the playcounts for the songs under that genre, then store it as a variable as I need to work with it. I'll give this a go... assuming I figure out how to implement it Smiley
Page Index Toggle Pages: 1