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 › use proxml for getting values out of a xml
Page Index Toggle Pages: 1
use proxml for getting values out of a xml (Read 608 times)
use proxml for getting values out of a xml
Nov 8th, 2009, 2:25pm
 
Hello guys,

I have a xml-file made looking like this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
 <ball>
   <width_ball ="45"/>
   <height_ball ="200"/>
 </ball>
 <ball>
   <width_ball ="100"/>
   <height_ball ="112"/>
 </ball>
</data>


I now want to write a processing sketch using proxml to read out all those values in the xml and put them into drawings. So basically I want to draw ellipses. The first ellipse should have a width of 45 and a height of 200, the second ellipse a width of 100 and a height of 112... and so on.
So how can I get the values out of the xml file?
I need to use proxml, as my whole project does not work fine with the processing xml.

My code started like this, can you give me a hand how i get the values  into it?

thanks a lot to you!

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

void setup(){
 size(500,500);
 
 xmlIO = new XMLInOut(this);
 xmlIO.loadElement("datafile.xml");
}

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

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

// i'm not sure if this is the right start. i thought of using a for-loop to go through the whole xml file and read out the data

// like e.g.:
// int widthBall = child[0];
// int heightBall = child[1];

}
}

void draw(){
}


Re: use proxml for getting values out of a xml
Reply #1 - Nov 9th, 2009, 6:26am
 
i found a solution to do this by using the processing.xml library. but i can't figure out, how to translate it into proxml code...

using processing-xml it looks like this:

BUT: when using this in a context with other code that uses proxml, it does not work anymore...so I want to translate this code into proxml-code.
for example: xml.getChildCount(); getChildCount is not a method one can use when working with proxml....


Code:
import processing.xml.*;
XMLElement xml;
final int SPACING_X = 10;
final int SPACING_Y = 10;
final int MAX_HEIGHT = 200;

void setup()
{
 size(800, 800);
 smooth();
 noLoop();
 background(200);
 Analyze();
 noStroke();
}

void Analyze() {
 noStroke();
 rectMode (CORNER);
 ellipseMode (CORNER);

   xml = new XMLElement(this, "data.xml");
   int numPics = xml.getChildCount();
   int posX = SPACING_X;
   int posY = SPACING_Y;
   int maxHeight = 0;
   for (int i = 0; i < numPics; i++)
   {
     XMLElement person = xml.getChild(i);
     XMLElement wEl = person.getChild(0);
     XMLElement hEl = person.getChild(1);

     int w = wEl.getIntAttribute("value_width");
     int h = hEl.getIntAttribute("value_height");
 
     // If new rectangle doesn't fit in the remainder of the line
     if (posX + w/2 > width - SPACING_X)
     {
       // Start a new line
       posX = SPACING_X;
       posY += SPACING_Y + maxHeight;
       maxHeight = 0;
     }
     // Keep track of tallest rectangle on this line
     // to get even spacing of lines (ie. smallest spacing)
     if (h > maxHeight) maxHeight = h;
     fill (255,255,255);

       ellipse (posX+50, posY, h*2, w);
     
     posX += w + SPACING_X;
 }
}
Re: use proxml for getting values out of a xml
Reply #2 - Nov 9th, 2009, 7:27am
 
First, your XML is broken... <width_ball ="45"/> is not a valid XML tag. It should be either <width_ball value="45"/> or <width_ball>45</width_ball>
The _ball part is, BTW, a bit redundant since this tag is inside a ball tag.

The proXML library seems broken when I need to get a textual value from a tag (the second form). I cannot find, in the library, an example of such XML form and even less an extraction of this value. All I get is a null value.
That, or I am too dumb to figure out how to do it...
I don't have time or will to debug the library to see who is wrong.

Anyway, I will take the easy solution, since you control the format of the XML file!
So, instead of
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
 <ball>
   <width>45</width>
   <height>200</height>
 </ball>
 <ball>
   <width>100</width>
   <height>112</height>
 </ball>
</data>

I will create
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
 <ball>
   <dimensions width="45" height="200"/>
 </ball>
 <ball>
   <dimensions width="100" height="112"/>
 </ball>
</data>

which becomes close of the example given on the site...
I can read this file with:
Code:
import proxml.*;

XMLInOut xmlIO;
ArrayList balls = new ArrayList();

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

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

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

 for (int i = 0; i < ballElts.length;i++)
 {
   proxml.XMLElement ballElt = ballElts[i];
   proxml.XMLElement param = ballElt.getChild(0);
   println(param.getName());
   if (!param.getName().equals("dimensions"))
continue; // Ignore unknown child
   int bw = param.getIntAttribute("width");
   int bh = param.getIntAttribute("height");
   println("Dim " + bw + " " + bh);
   Ball ball = new Ball(bw, bh);
   balls.add(ball);
 }
}

void draw()
{
 background(255);
 println(balls.size());
 for (int i = 0; i < balls.size(); i++)
 {
   Ball b = (Ball) balls.get(i);
   b.display();
 }
}

class Ball
{
 int ballWidth;
 int ballHeight;
 int x, y;
 color c;
 
 Ball(int bw, int bh)
 {
   ballWidth = bw;
   ballHeight = bh;
   x = int(random(bw, width - bw));
   y = int(random(bh, width - bh));
   c = color(random(50, 200), random(100, 200), random(200, 255));
 }
 
 void display()
 {
   fill(c);
   noStroke();
   ellipse(x, y, ballWidth, ballHeight);
 }
}

Funnily, I wanted to use a simple array and append() but I couldn't make Processing to append to an empty array... Oh well.
Page Index Toggle Pages: 1