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.
IndexProcessing DevelopmentLibraries,  Tool Development › ProXML and the PApplet
Page Index Toggle Pages: 1
ProXML and the PApplet (Read 1566 times)
ProXML and the PApplet
Mar 10th, 2006, 7:31pm
 
Hi,

I have successfully worked with this proXML example - http://texone.org/proxmlapplet/proxml_test.pde

Now I have been trying to move the XML parsing code to a different class that does not have a setup() and I can not get it to work. It seems this line of code:

xmlInOut = new XMLInOut(this);

…has to be in setup() so the “this” resolves to the PApplet. OK – is there a way to load XML data into a different class that does not have a setup().

Example:

newClass nc1 = new newClass ();

void setup() {
 size(1280, 1000, P3D);
 //background(88,88,88);
 framerate(25);
 noStroke();
 loop();
}
void draw() {
}

class newClass{
 newClass(){
 //LOAD THE XML DATA HERE
 }
}

Thanks,

4dplane
Re: ProXML and the PApplet
Reply #1 - Mar 10th, 2006, 10:44pm
 
Hi

Try it this way

Code:


newClass nc1;
newClass2 nc2;

XMLInOut xmlInOut;

void setup() {
size(1280, 1000, P3D);
//background(88,88,88);
framerate(25);
noStroke();
loop();

xmlInOut = new XMLInOut(this);

//be aware you have to initialize xmlinout first
//otherwise you would get a nullpointerexception
nc1 = new newClass ();

//here you give an reference to xmlInOut
//directly to the class you have to it this way
//if your class is defined in an java file
nc2 = new newClass2(xmlInOut);
}

void draw() {
}

class newClass{

XMLElement xmlElement;

newClass(){
xmlElement= xmlInOut.loadElementFrom("xmlElement.xml");
}
}

// other way
class newClass2{

XMLElement xmlElement;
XMLInOut xmlInOut;

newClass(XMLInOut xmlInOut){
this.xmlInOut = xmlInOut;
xmlElement= xmlInOut.loadElementFrom("xmlElement.xml");
}
}
Re: ProXML and the PApplet
Reply #2 - Mar 11th, 2006, 9:17am
 
Thanks, I have been working with the second way you described and this is the error I can't seem to work around:

C:/DOCUME~1/main/LOCALS~1/Temp/build33363.tmp/Temporary_4521_8302.java:41:15:41:
49: Semantic Error: The method "proxml.XMLElement loadElementFrom(java.lang.String $1) throws proxml.InvalidDocumentException;" can throw the checked exception "proxml.InvalidDocumentException", so its invocation must be enclosed in a try statement that catches the exception, or else this constructor must be declared to throw the exception.

error from

class newClass{
 XMLElement dataSet;
 XMLElement data;
 XMLElement source;
 XMLInOut xmlInOut;
 newClass(XMLInOut xmlInOut){
   this.xmlInOut = xmlInOut;
   dataSet = xmlInOut.loadElementFrom("dat.xml"); //<-error
}
}
Re: ProXML and the PApplet
Reply #3 - Mar 11th, 2006, 9:46am
 
Oh I forgot

You have to handle the exception. Exceptions are usefull to handle errors that can appear in your programms.

Code:

class newClass{
XMLElement dataSet;
XMLElement data;
XMLElement source;
XMLInOut xmlInOut;

newClass(XMLInOut xmlInOut){
this.xmlInOut = xmlInOut;

try{
dataSet = xmlInOut.loadElementFrom("dat.xml"); //<-error
}catch(InvalidDocumentException ide){
println("Problems on loading dat.xml");
}
}
}


xmlInOut.loadElementFrom throws an exception if it cannot load the element, so you have to define how this exception has to be handled. To do that you set the code part that can throw an exception in the try block. in the catch block you can define what has to be done if an error appears.
Re: ProXML and the PApplet
Reply #4 - Mar 12th, 2006, 3:04am
 
Perfect!

Thanks tex,

4dplane
Page Index Toggle Pages: 1