XML example is finalized and now mirrors Table example

This commit is contained in:
Daniel Shiffman
2013-03-11 13:21:10 -04:00
parent 426e9a62eb
commit 42d8064804
2 changed files with 55 additions and 19 deletions

View File

@@ -57,7 +57,6 @@ void loadData() {
for (int i = 0; i < bubbles.length; i++) {
// The position element has two attributes: x and y
XML positionElement = children[i].getChild("position");
// Note how with attributes we can get an integer or float directly
@@ -81,24 +80,39 @@ void loadData() {
// Still need to work on adding and deleting
/*void mousePressed() {
// Create a new row
TableRow row = table.addRow();
// Set the values of that row
row.setFloat("x", mouseX);
row.setFloat("y", mouseY);
row.setFloat("diameter", random(40, 80));
row.setString("name", "Blah");
void mousePressed() {
// If the table has more than 10 rows
if (table.getRowCount() > 10) {
// Delete the oldest row
table.removeRow(0);
// Create a new XML bubble element
XML bubble = xml.addChild("bubble");
// Set the poisition element
XML position = bubble.addChild("position");
// Here we can set attributes as integers directly
position.setInt("x",mouseX);
position.setInt("y",mouseY);
// Set the diameter element
XML diameter = bubble.addChild("diameter");
// Here for a node's content, we have to convert to a String
diameter.setContent("" + random(40,80));
// Set a label
XML label = bubble.addChild("label");
label.setContent("New label");
// Here we are removing the oldest bubble if there are more than 10
XML[] children = xml.getChildren("bubble");
// If the XML file has more than 10 bubble elements
if (children.length > 10) {
// Delete the first one
xml.removeChild(children[0]);
}
// Writing the CSV back to the same file
saveTable(table,"data/data.csv");
// And reloading it
// Save a new XML file
saveXML(xml,"data/data.xml");
// reload the new data
loadData();
}*/
}

View File

@@ -1 +1,23 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<bubbles>
<bubble>
<position x="160" y="103"/>
<diameter>43.19838</diameter>
<label>Happy</label>
</bubble>
<bubble>
<position x="372" y="137"/>
<diameter>52.42526</diameter>
<label>Sad</label>
</bubble>
<bubble>
<position x="273" y="235"/>
<diameter>61.14072</diameter>
<label>Joyous</label>
</bubble>
<bubble>
<position x="121" y="179"/>
<diameter>44.758068</diameter>
<label>Melancholy</label>
</bubble>
</bubbles>