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 › The constructor for XMLElement is Undefined
Page Index Toggle Pages: 1
The constructor for XMLElement is Undefined (Read 1585 times)
The constructor for XMLElement is Undefined
Apr 10th, 2009, 4:59am
 
Hi,
I'm trying to build up a class to read NikePlus xml and store it.
I get this error if i try to run it "XMLElement is Undefined "; I guess it is connected with PApplet location but i don't know what to use instead of "this" and why.
Can you explain me what to do to make it working?
Thanks.

Code:
/*=====================================
*Nike Plus Importer v.0.1
*===================================*/


class NikePlusImporter{
 
 /*====================================
  *XML storing variables
  *==================================*/
 XMLElement NikePlus; //entire XML
 XMLElement [ ] RunList; //run List array
 String url = "nikeplus.nike.com/nikeplus/v1/services/widget/get_public_run_list.jsp?userID=";
 
 
 /*======================================
  *Constructor
  *load XML;
  *string RunnerID,
  ======================================*/
 NikePlusImporter(String Id){
   String UrlNike =(url + Id);

   NikePlus = new XMLElement(this, UrlNike);
   RunList = NikePlus.getChildren("runList/run");
   int numRun = RunList.length;
   println(numRun);
 }

}
Re: The constructor for XMLElement is Undefined
Reply #1 - Apr 10th, 2009, 5:11am
 
which version of processing are you using? .. earlier versions had xml included as a library, you needed to add "import processing.xml.*;" at the top of your sketch.
Re: The constructor for XMLElement is Undefined
Reply #2 - Apr 10th, 2009, 5:26am
 
I had problems with some xml loading in Processing because of the usual xml header (and I can't remember if it was this error, maybe).

Removing the first 3 characters of the xml file made it running, but I don't know how to make it at runtime instead of editing the xml file... ?
A fix for the library might be nice too.

("<?xml version="1.0" encoding="utf-8"?>" -> see the 3 first characters "" ).
Re: The constructor for XMLElement is Undefined
Reply #3 - Apr 10th, 2009, 3:39pm
 
I'm using 1.0.3,
If i try to read the XML in draw the same code work just fine i have problem only from the class.
Re: The constructor for XMLElement is Undefined
Reply #4 - Apr 10th, 2009, 11:53pm
 
that was so obvious .. tja.

"this" in your case referrs to an instance of "NikePlusImporter" (from where you call it) but XMLElement needs a "PApplet" (your sketch). There are several way to fix it: you can put a global function to pass back the PApplet, like:

PApplet getParent ()
{
    return this;
}

then you can do:

NikePlus = new XMLElement( getParent(), UrlNike);

... another way to do it is to pass your sketch into the constructor of the NikePlusImporter class.

F
Re: The constructor for XMLElement is Undefined
Reply #5 - Apr 11th, 2009, 5:35am
 
That's the point.
I've understand that "this" is referring to the main sketch but I don't know what to put instead.

I've tried the way you have suggested; it doesn't gave the error any more but it neither run the sketch, it seems to get stuck before the sketch window appear, don't know why.
What about the second way?
Do you know a tutorial or something where I could learn about it?

Thanks
Re: The constructor for XMLElement is Undefined
Reply #6 - Apr 11th, 2009, 10:40am
 
no error means that's solved. are you sure that the xml is available to be read? i'd try to println the contents first .. and add "http://" to the url.

here's "this" in the processing ref:
http://processing.org/reference/this.html

F
Re: The constructor for XMLElement is Undefined
Reply #7 - Apr 11th, 2009, 12:12pm
 
The xml is fine if you try to run it all from the main sketch it work nice.

I can't put "http" on the code below, cause I've wrote less than 5 post so I'm not allowed to put web link on the post, on the real code it's there Wink.

Code:
  XMLElement NikePlus; //entire XML
 XMLElement [ ] RunList; //run List array
 String url = "nikeplus.nike.com/nikeplus/v1/services/widget/get_public_run_list.jsp?userID=";
 
 String RunnerId = "751872695";    //ID of Nike+ Runner

   String UrlNike =(url + RunnerId);

   NikePlus = new XMLElement(this, UrlNike);
   RunList = NikePlus.getChildren("runList/run");
   int numRun = RunList.length;
   println(numRun);
   
   


Instead if you try to run it with the class implementation it get stucked.
Full code below:

Code:
String RunnerId = "751872695";    //ID of Nike+ Runner
NikePlusImporter nike;

void setup(){
size(200,200);
//noLoop();  
}

void draw()   {
 
 nike = new NikePlusImporter(RunnerId);
 
 
 
}

PApplet getParent ()
{
   return this;
}



class NikePlusImporter{
 
 /*====================================
  *XML storing variables
  *==================================*/
 XMLElement NikePlus; //entire XML
 XMLElement [ ] RunList; //run List array
 String url = "nikeplus.nike.com/nikeplus/v1/services/widget/get_public_run_list.jsp?userID=";
 
 
 /*======================================
  *Constructor
  *load XML;
  *string RunnerID,
  ======================================*/
 NikePlusImporter(String Id){
   String UrlNike =(url + Id);

   NikePlus = new XMLElement(getParent(), UrlNike);
   RunList = NikePlus.getChildren("runList/run");
   int numRun = RunList.length;
   println(numRun);
 }

}

Re: The constructor for XMLElement is Undefined
Reply #8 - Apr 11th, 2009, 12:53pm
 
ah, i see. that was my fault .. getParent was a bad choice for the function name and caused a stack overflow. here's the working code:

Code:
String RunnerId = "751872695";    //ID of Nike+ Runner
NikePlusImporter nike;

void setup(){
size(200,200);
//noLoop();

nike = new NikePlusImporter(RunnerId);
}

void draw() {



}

PApplet getPapplet ()
{
return this;
}



class NikePlusImporter{

/*====================================
*XML storing variables
*==================================*/
XMLElement NikePlus; //entire XML
XMLElement [ ] RunList; //run List array
String url = "http://nikeplus.nike.com/nikeplus/v1/services/widget/get_public_run_list.jsp?userID=";


/*======================================
*Constructor
*load XML;
*string RunnerID,
======================================*/
NikePlusImporter(String Id){
String UrlNike =(url + Id);

NikePlus = new XMLElement(getPapplet(), UrlNike);
RunList = NikePlus.getChildren("runList/run");
int numRun = RunList.length;
println(numRun);
}

}


F
Re: The constructor for XMLElement is Undefined
Reply #9 - Apr 11th, 2009, 4:53pm
 
I've fix it  . I've made the NikePlusImporter class an extension of the PApplet, that basically is what the main sketch is,  so "this" work again.

I still need to figuring out how I can point to a different class instead of using "this"; time will help Smiley.
Re: The constructor for XMLElement is Undefined
Reply #10 - Apr 12th, 2009, 2:14am
 
WeberFederico wrote on Apr 11th, 2009, 4:53pm:
I still need to figuring out how I can point to a different class instead of using "this"; time will help Smiley.

I think you cannot: you give "this" to XMLElement class to let it access to PApplet fields and methods.
If you put your class in an independent file (.java), you have to pass the PApplet instance to your class, usually in the constructor, or by using a setter.
Something like:

NikePlusImporter npi = new NikePlusImporter(this, id);

on the sketch's side, and:

NikePlusImporter(PApplet pa, String Id){
   String UrlNike =(url + Id);
   NikePlus = new XMLElement(pa, UrlNike);
   // You can put 'pa' in a field of NikePlusImporter if you need it later


in your class.
Re: The constructor for XMLElement is Undefined
Reply #11 - Apr 13th, 2009, 2:12am
 
This is clever, Is the same Fjen said, about passing the sketch in the constructor, but I didn't understood it at first Smiley.
Thanks.
Page Index Toggle Pages: 1