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 & HelpPrograms › Parsing Wikipedia XML file
Page Index Toggle Pages: 1
Parsing Wikipedia XML file (Read 619 times)
Parsing Wikipedia XML file
Nov 20th, 2008, 1:38pm
 
Hi,

I would like to display the comments of all <rev>-nodes of a wikipedia XML file. I don't know why, but my children array must be empty because I get an IndexOutOfBounds Exception (= 0) when I want to print the array content.

Any ideas?

Thanks
Marcus

Code:

import processing.core.*;
import processing.xml.*;

public class Wikipedia04 extends PApplet {

public static void main ( String args[] ) {
PApplet.main( new String[]{"Wikipedia04"} );
}

public void setup ()
{




background(0);

size(100,100);



String url = "http://de.wikipedia.org/w/api.php?format=xml&action=query&#8733;=revisions&titles=Burg&rvprop=timestamp|user|comment|size|flags&rvlimit=100";



XMLElement xml = new XMLElement(this, url);


XMLElement[] children = xml.getChildren("api/query/pages/page/revisions");

for (int i=0; i <= xml.getChildCount(); i++) {


println(children[i].getStringAttribute("comment"));

}

}

public void draw ()
{

}

}
Re: Parsing Wikipedia XML file
Reply #1 - Nov 20th, 2008, 5:24pm
 
In Java, to iterate over an array or collection, you always go from 0 (included) to size/length (excluded).

So your loop should look like:

for (int i=0; i < xml.getChildCount(); i++)

If it doesn't enter the loop, that's indeed that you have a count of 0.
Re: Parsing Wikipedia XML file
Reply #2 - Nov 20th, 2008, 5:37pm
 
I found several things in your code:

1.
There were some strange chars in the url "...&#8733;=revisions&...", instead of "..."prop=revisions&..."

2.
For some reason, that I also do not understand completely, the path you give in getChildren has be like that:
"query/pages/page/revisions/rev"
So, without "api/"

3.
You must not iterate over xml.getChildCount() because that's again the root node.

The following code worked for me in Processing (0157):

Code:

import processing.core.*;
import processing.xml.*;

void setup () {
size(100,100);
background(0);

String url = "http://de.wikipedia.org/w/api.php?format=xml&action=query&prop=revisions&titles=Burg&rvprop=timestamp|user|comment|size|flags&rvlimit=100";

XMLElement xml = new XMLElement(this, url);
XMLElement[] children = xml.getChildren("query/pages/page/revisions/rev");

for (int i=0; i < children.length; i++) {
println(children[i].getStringAttribute("comment"));
}

}



Re: Parsing Wikipedia XML file
Reply #3 - Nov 20th, 2008, 10:51pm
 
Thanks a lot for your help. It works with Processing 155, too.

Marcus
Page Index Toggle Pages: 1