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 › proXML - go through an external xml file
Page Index Toggle Pages: 1
proXML - go through an external xml file (Read 1532 times)
proXML - go through an external xml file
Jan 15th, 2010, 4:59am
 
hello guys,

i've read through the the examples in the proxml documentation and i have y question concerning this one:

Code:
/*
This example loads an xml file and
prints out the children of the root element
*/

import proxml.*;
import proxml.XMLElement;

XMLInOut xmlIO;

void setup(){
size(400,400);

xmlIO = new XMLInOut(this);
xmlIO.loadElement("daten.xml");
}

void xmlEvent(XMLElement element){
proxml.XMLElement[] children = element.getChildren();

for(int i = 0; i < children.length;i++){
proxml.XMLElement child = children[i];
println(child);
}
}
void draw(){
}


This example includes the function xmlEvent. In my own script I have several functions that all go through xmlFiles but I want to give them other names and I want to call them by the time I need them. So how do I actually call this function, because in the script above there is nothing like "xmlEvent(); to call this function.
In my case, I have a function called "SecondXml()" and it is of the same structure as the xmlEvent() function. But I can't call my function. I've tried it his way, but it does not work:

Code:


SecondXml (XMLElement element);
void SecondXml(XMLElement element) {

}


Can anyone give me a hint?
Thanks a lot !!
Re: proXML - go through an external xml file
Reply #1 - Jan 15th, 2010, 5:12am
 
xmlEvent is probably a callback function, so its name is mandatory and at global level (eg. cannot be inside a class).
I fear you have to go through this one. A bit like mousePressed and similar Processing functions.

But now, you can dispatch the behavior from there:
Code:
void xmlEvent(XMLElement element) {
switch (currentLexer) {
case FIRST: FirstXML(element); break;
case SECOND: SecondXML(element); break;
// and so on
}
}

When you need to activate one lexer, just assign the desired constant to currentLexer variable.
Re: proXML - go through an external xml file
Reply #2 - Jan 15th, 2010, 7:33am
 
okay, i tried it your way and it works - but still there's one bug.

I solved it this way:
Code:
void xmlEvent(XMLElement element){
if (xmlCheck == false) {

FristXML (element);
}
if (xmlCheck == true) {

SecondXML (element);

}
}


and by using mousePressed I change between true and false. But: when I changed xmlCheck from false to true the function SecondXML is not called.

In your example what is currentLexer needed for?
Re: proXML - go through an external xml file
Reply #3 - Jan 15th, 2010, 12:35pm
 
I would have said FristXML wouldn't be called instead... Smiley
You can write:
Code:
void xmlEvent(XMLElement element){
if (xmlCheck) {
SecondXML(element);
} else {
FirstXML(element);
}
}

How do you toggle xmlCheck?

I explain currentLexer in my last sentence, it was made with more than two lexers in mind, so that you do: currentLexer = FIRST; or currentLexer = SECOND; or currentLexer = THIRD; and so on.
Re: proXML - go through an external xml file
Reply #4 - Jan 15th, 2010, 4:41pm
 
Ok I tried to reduce the code as much as possible...
if you just copy this into Processing you might see the problem?
I use mouseClicked to toggle xmlCheck. So xmlCheck starts up with "false" so actually FirstXML should work. Don't know why it doesn't.

And when clicking the mouse Processing should flip between FirstXML and SecondXML...


Code:
 import proxml.*;
import proxml.XMLElement;

boolean xmlCheck = false;

void setup() {
}

void draw() {
}

void xmlEvent(XMLElement element){
if (xmlCheck == true) {
SecondXML(element);
}
else {
FirstXML(element);
}
}


void FirstXML (XMLElement element) {
println ("FirstXML running");
}

void SecondXML (XMLElement element) {
println ("SecondXML running");
}

void mouseClicked () {
if (xmlCheck == true) {
xmlCheck = false;
}
else if (xmlCheck == false) {
xmlCheck = true;

}
}

Re: proXML - go through an external xml file
Reply #5 - Jan 16th, 2010, 12:05am
 
Quote:
if you just copy this into Processing you might see the problem?
Not really, also need to parsing code... Wink (Yeah, it is above...)

Basically, the logic seems OK. I would add some println() to show the value of xmlCheck, one at the end of mouseClicked, another at the start of xmlEvent.
Re: proXML - go through an external xml file
Reply #6 - Jan 16th, 2010, 1:17am
 
Ok now when executing this script you see the bug. When starting it goes straight into FirstXML. When clicking I'll print out if xmlCheck is true or false - but it does not switch between "FirstXML()" and "SecondXML()".  I don't know why but I think it's because I do not call xmlEvent (); at any time? That's the point I dont get Sad
Code:
import proxml.*;
import proxml.XMLElement;

XMLInOut xmlInOut;
boolean xmlCheck = false;

void setup() {
 xmlInOut = new XMLInOut(this);
 try{
   xmlInOut.loadElement("data1.xml");
 }
 catch(Exception e){
   //if the xml file could not be loaded it has to be created
   xmlEvent(new XMLElement("data"));

 }
}

void draw() {
}

void xmlEvent(XMLElement element){
 println(xmlCheck);
 if (xmlCheck == true) {
   SecondXML(element);
 }
 else {
   FirstXML(element);
 }
}


void FirstXML (XMLElement element) {
 println ("FirstXML running");
}

void SecondXML (XMLElement element) {
 println ("SecondXML running");
}

void mouseClicked () {
 if (xmlCheck == true) {
   xmlCheck = false;
 }
 else if (xmlCheck == false) {
   xmlCheck = true;

 }
 println(xmlCheck);
}


Re: proXML - go through an external xml file
Reply #7 - Jan 16th, 2010, 4:30am
 
Quote:
the function SecondXML is not called
Quote:
FirstXML should work. Don't know why it doesn't.
Quote:
it goes straight into FirstXML

Now, I am quite confused...  Shocked

Anyway.
The XML read and processing is nearly instantaneous. By the time you click the mouse, all the treatment is already finished. So xmlEvent won't be called anymore.
If you want to re-process data1.xml again, you probably have to do again loadElement() on mouse click.

Re: proXML - go through an external xml file
Reply #8 - Jan 16th, 2010, 11:15am
 
thx a lot, by using loadElement() again it works perfectly well Smiley
Page Index Toggle Pages: 1