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.