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 › XML parsing - object vs. array
Page Index Toggle Pages: 1
XML parsing - object vs. array? (Read 887 times)
XML parsing - object vs. array?
Dec 5th, 2009, 5:22pm
 
I'm still very new to processing. I'm having some trouble working with XML.

I have the following XML:

Code:
<recenttracks user="RJ" page="1" perPage="10" totalPages="3019">
 <track>
   <artist>Aretha Franklin</artist>
   <name>Sisters Are Doing It For Themselves</name>
   <date uts="1213031819">9 Jun 2008, 17:16</date>
 </track>
 <track>
   <artist>Radiohead</artist>
   <name>Karma Police</name>
   <date uts="1213031819">9 Jun 2008, 17:13</date>
 </track>
</recenttracks>


I want to loop through each <track>, displaying the <artist> and other children.

My issue is very simple, I think..  I'm running into a problem with getChildren. Below is my code:

Code:
xml = new XMLElement(this,"data.xml");
 
 XMLElement tracks = xml.getChildren();
   
   for (int i = 0; i < numTracks; i++) {

XMLElement track = tracks.getChild(i);

XMLElement artist = track.getChild("track/artist");
String artistName = artist.getContent();
println("1: " + artistName);    
   }


this is my attempt to dig one level into my XML so that I'm working with just <track> elements instead of <recenttracks>. The error I get: "cannot convert from XMLElement[] to XMLElement". What I understand is that it's trying to convert from an object to an array. However, when I try this instead, I get another error:

 XMLElement[] tracks = xml.getChildren();

The error is "cannot invoke getChild(int) on the array type XMlElement[]"

So I'm not sure where to go from here, but I'm sure there's a better way.

Any help would be greatly appreciated, let me know if you need any more information!
Re: XML parsing - object vs. array?
Reply #1 - Dec 5th, 2009, 5:57pm
 
I was dealing with this kind of xml program a week or so a go. One of the first thing I noticed is that your mixing up tag content with a tag attribute. These serve the same function except that content can be nested where as attributes can not. They are called differently (getAttribute() and get getContent() respectively, your method requiring the latter).

This is a non recursive solution for your problem (which means it is limited to this three level nested structure, but I have found it to be a bit less mind bending). Hope it helps.

void setup()
{
 size(200,200);
 XMLElement xml = new XMLElement(this, "test.xml");
 XMLElement xmlChild = xml.getChild(1); //gets you into the root folder this can be hard number since there can only be one root folder
 int count = xmlChild.getChildCount();//counts songs
 for (int i= 0; i < count; i++)
 {
   int attCount = xmlChild.getChildCount();//counts song info types, which are children too
   for (int j = 0; j < attCount; j++)
   {
     XMLElement xmlGrandChild = xmlChild.getChild(j);
     println(xmlGrandChild.getContent());
   }
 }
}


To make the program more elegant you could consider making the content attributes (you could lose one for loop that way because you only have one level of children). That means
<track name="Ooh" length = "2:36"></track>
instead of
<track>
 <name>Ooh</name>
 <length>2:36</lenght>
</track>
Re: XML parsing - object vs. array?
Reply #2 - Dec 5th, 2009, 6:35pm
 
Looks like that one works perfectly! Only issue was in the xmlChild definition, it needs to refer to the first element (0) rather than (1).

Code:
XMLElement xmlChild = xml.getChild(0); 



Thanks a lot!
Re: XML parsing - object vs. array?
Reply #3 - Dec 5th, 2009, 6:38pm
 
Glad I could help.

Deurbel
Page Index Toggle Pages: 1