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 › read XML from applet
Page Index Toggle Pages: 1
read XML from applet (Read 1601 times)
read XML from applet
May 11th, 2010, 7:21pm
 
I have a program that reads XML data which is gathered and updated daily using a cronjob on my server. The problem is that when Processing  exports a Java applet it seems to embed the XML data into the code instead of keeping it independent. How can I have it function as an applet on the web but still read from XML stored on my server

My program is here: http://zakgreene.com/deep
Re: read XML from applet
Reply #1 - May 11th, 2010, 11:50pm
 
You read the XML files stored in the data folder of your sketch.
You have to specify instead an URL to a place on your server, something like state1xml = new XMLElement(this, "http://zakgreene.com/deep/data/state1.xml");
And of course, your cron job must write at this location, converted to a path, like /<some root path>/www/deep/data
Re: read XML from applet
Reply #2 - May 12th, 2010, 12:13am
 
Great! Thanks!

I've run into another somewhat related bug I wonder if you could help with.

I'm trying now to delve into objects, which I was previously unfamiliar with, but I think it will help my program and make it easier to add and expand. I'd been using this basic syntax in setup() to read my XML files:

Code:
xml = new XMLElement(this, "state1.xml"); 



Yet now I'm trying to do that within an object called Shape. So I'm getting an error because "this" now referes to the object Shape. This is probably a no-brainer but I can't for the life of me figure it out. How do I refer to the parent object, or in this case, the sketch itself. I can't find an example of anything other than "this" as the first argument to XMLElement().
Re: read XML from applet
Reply #3 - May 12th, 2010, 12:55am
 
Also, it doesn't seem to work. It seems to include the data from the XML in the java applet files themselves. I uploaded the original applet which points to a URL, then modified the file on my server without reuploading the applet and the XML data didn't change. The idea is that the cronjob updates this file and I don't have to export it from my computer every time...
Re: read XML from applet
Reply #4 - May 12th, 2010, 4:44am
 
this refers to the current object, so if you are in a Shape class, it refers to the current instance of Shape, as you found out.
Libraries show to pass this as parameter outside of classes, so it refers to the current PApplet instance, automatically created by Processing.
The classical way to handle the problem is to do like libraries, and make the constructor of your class to accept a PApplet parameter:
Code:
// Object creation in the sketch:
Shape sh = new Shape(this, "state1.xml");

// Constructor of the class:
public Shape(PApplet pa, String fileName) // Others params possible, of course
{
xml = new XMLElement(pa, fileName);
// Etc.
}
Re: read XML from applet
Reply #5 - May 12th, 2010, 4:46am
 
ZakG wrote on May 12th, 2010, 12:55am:
It seems to include the data from the XML in the java applet files themselves. I uploaded the original applet which points to a URL, then modified the file on my server without reuploading the applet and the XML data didn't change.

Don't forget to clear the Java cache, otherwise applets don't get reloaded after changes, Java will just get the old jar from its cache.
Re: read XML from applet
Reply #6 - May 12th, 2010, 1:11pm
 
Perfect! Thank you!
Page Index Toggle Pages: 1