Mmm, despite the title, I used the built-in XML parser of Processing.
If you don't mind, I would say your XML structure is a bit convoluted. Now, XML design isn't simple, there isn't a single road, and I am not an expert, so take or leave my advice.
To wet my feet, I made my own structure(s) of your data, then I tried to read you.
The XML files:
mine1.xml (my preferred version...)
Code:<?xml version="1.0" encoding="ISO-8859-1"?>
<pics>
<pic width="190" height="90"/>
<pic width="30" height="30"/>
<pic width="100" height="180"/>
<pic width="277" height="130"/>
</pics>
mine2.xml (actually the first simplification I made)
Code:<?xml version="1.0" encoding="ISO-8859-1"?>
<pics>
<pic>
<width>190</width>
<height>90</height>
</pic>
<pic>
<width>30</width>
<height>30</height>
</pic>
<pic>
<width>100</width>
<height>180</height>
</pic>
<pic>
<width>277</width>
<height>130</height>
</pic>
</pics>
original.xml Code:(as in your message!)
Now, the code I used to parse these files:
Code:XMLElement xml;
final int SPACING = 30;
void setup()
{
size(800, 300);
smooth();
noLoop();
background(#AAFFEE);
fill(#AAFF00);
ReadMine1();
fill(#BBEE00);
ReadMine2();
fill(#CCDD00);
ReadOriginal();
}
void ReadMine1()
{
xml = new XMLElement(this, "mine1.xml");
int numPics = xml.getChildCount();
int pos = SPACING;
for (int i = 0; i < numPics; i++)
{
XMLElement pic = xml.getChild(i);
int w = pic.getIntAttribute("width");
int h = pic.getIntAttribute("height");
// println("Got: " + w + " " + h);
rect(pos, SPACING, w, h);
pos += w + SPACING;
}
}
void ReadMine2()
{
xml = new XMLElement(this, "mine2.xml");
int numPics = xml.getChildCount();
int pos = 2 * SPACING;
for (int i = 0; i < numPics; i++)
{
XMLElement pic = xml.getChild(i);
// Warning: this is brittle,
// relying on order of tags in XML file
XMLElement wEl = pic.getChild(0);
XMLElement hEl = pic.getChild(1);
int w = int(wEl.getContent());
int h = int(hEl.getContent());
// println("Got: " + w + " " + h);
rect(pos, 2 * SPACING, w, h);
pos += w + SPACING;
}
}
void ReadOriginal()
{
xml = new XMLElement(this, "original.xml");
int numPics = xml.getChildCount();
int pos = 3 * SPACING;
for (int i = 0; i < numPics; i++)
{
XMLElement pic = xml.getChild(i);
XMLElement wEl = pic.getChild(0);
XMLElement hEl = pic.getChild(1);
int w = wEl.getIntAttribute("pw");
int h = hEl.getIntAttribute("ph");
println("Got: " + w + " " + h);
rect(pos, 3 * SPACING, w, h);
pos += w + SPACING;
}
}
I like this site because each question make me explore a library or some algorithms or some function...