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 & HelpOther Libraries › Problem accessing XML with applet-proXML
Page Index Toggle Pages: 1
Problem accessing XML with applet-proXML (Read 1732 times)
Problem accessing XML with applet-proXML
Mar 29th, 2010, 6:39pm
 
Hi everyone.

I've been experimenting with proXML, hoping to create a persistent web applet.

One problem I've run into though is that once you compile a sketch into an applet (just using the default ellipse example below), the XML file appears to be integrated into the java file.
This prevents me from editing the XML file, so that the processing file is no longer persistent, as it was as a sketch.

Is anyone aware of any work arounds to this problem? Either with proXML or the core XML library?

Thanks for your time!





//      ellipse.xml code

<?xml version="1.0"?>
<ellipses>
 <ellipse>
   <position yPos="152" xPos="127"/>
   <size Xsize="61" Ysize="54"/>
 </ellipse>
 <ellipse>
   <position yPos="155" xPos="212"/>
   <size Xsize="33" Ysize="66"/>
 </ellipse>
 <ellipse>
   <position yPos="246" xPos="130"/>
   <size Xsize="65" Ysize="48"/>
 </ellipse>
</ellipses>





//      ellipse.pde code
import proxml.*;

//to store the background after painting an ellipse
PImage back;

//xml element to store and load the drawn ellipses
XMLElement ellipses;
XMLInOut xmlInOut;

int xPos = 0;
int yPos = 0;

void setup(){
 size(400,400);
 smooth();
 background(255);

 //load ellipses from file if it exists
 xmlInOut = new XMLInOut(this);
 try{
   xmlInOut.loadElement("ellipse.xml");
 }catch(Exception e){
   //if the xml file could not be loaded it has to be created
   xmlEvent(new XMLElement("ellipses"));
 }
}

void xmlEvent(XMLElement element){
 ellipses = element;
 initEllipses();
   //initialise PImage for background
 back = new PImage(width,height);
 loadPixels();
 back.pixels = pixels;
}

void draw(){
}

//draw all ellipses saved in the xml file
void initEllipses(){
 ellipses.printElementTree(" ");
 XMLElement ellipse;
 XMLElement position;
 XMLElement size;
 
 for(int i = 0; i < ellipses.countChildren();i++){
   ellipse = ellipses.getChild(i);
   position = ellipse.getChild(0);
   size = ellipse.getChild(1);
   ellipse(
     position.getIntAttribute("xPos"),
     position.getIntAttribute("yPos"),
     size.getFloatAttribute("Xsize"),
     size.getFloatAttribute("Ysize")
   );
 }
}

void mousePressed(){
 xPos = mouseX;
 yPos = mouseY;
}

void mouseDragged(){
 background(back);
 ellipse(xPos,yPos,abs(xPos-mouseX),abs(yPos-mouseY));
}

void mouseReleased(){
 XMLElement ellipse = new XMLElement("ellipse");
 ellipses.addChild(ellipse);
 XMLElement position = new XMLElement("position");
 position.addAttribute("xPos",xPos);
 position.addAttribute("yPos",yPos);
 ellipse.addChild(position);
 XMLElement size = new XMLElement("size");
 size.addAttribute("Xsize",abs(xPos-mouseX));
 size.addAttribute("Ysize",abs(yPos-mouseY));
 ellipse.addChild(size);
 xmlInOut.saveElement(ellipses,"ellipse.xml");
 loadPixels();
 back.pixels = pixels;
}
Re: Problem accessing XML with applet-proXML
Reply #1 - Mar 30th, 2010, 4:40am
 
If you make an applet out of your sketch, it looses the capability to save back the XML anyway.
Well, actually you might be able to save a file on user's hard disk, but you need to sign your applet for that (for security reasons).
Or you have to save back the file to the server providing the applet (look at the postToWeb library).
Re: Problem accessing XML with applet-proXML
Reply #2 - Mar 30th, 2010, 8:47pm
 
Thanks for the advice, PhiLho!
I took inspiration from your last bit of advice and managed to cobble together some PHP scripting that manipulated the XML data for me.


I have one more question though; is it possible for a Processing applet to read an XML file in the same subdirectory?

So far I've only been able to get the applet to read the XML file that it's been exported with, and that's a bit problematic since I want the applet and PHP script to share the same XML file.


Nevermind, I was just absent minded enough not to turn grant all the correct permissions. Hehehe.
Page Index Toggle Pages: 1