Count number of specific entries in XML
in
Core Library Questions
•
2 years ago
Hello,
Using XMLElement I would like to find the number of times that "zombie" appears in the <state> tag (see example code below).
I can use .getChildCount() to find how many children there are overall, so it seems like there'd be some way to add a simple selector so that it only pulled the children I specify. I've played around with the other .get operators and I haven't gotten any results. Thoughts?
<players>
<name id="1">Brian.Klingenfus</name>
<state>human</state>
<kills>0</kills>
<fed>2010-11-09 23:45:33</fed>
<name id="2">Allyson.Johnson ?</name>
<state>human</state>
<kills>0</kills>
<fed>2010-11-09 23:47:06</fed>
<name id="3">Justin.Clatterbuck ?</name>
<state>human</state>
<kills>0</kills>
<fed>2010-09-20 00:32:00</fed>
<name id="9">Debel Issttas</name>
<state>zombie</state>
<kills>1</kills>
<fed>2010-09-17 04:02:00</fed>
<name id="10">Stad Angtia</name>
<state>zombie</state>
<kills>2</kills>
<fed>2010-09-18 12:00:00</fed>
<name id="11">Ashet Enild</name>
<state>zombie</state>
<kills>0</kills>
<fed>2010-11-09 23:45:33</fed>
<name id="12">Sokel Ildup</name>
<state>zombie</state>
<kills>2</kills>
<fed>2010-11-09 23:47:06</fed>
<name id="13">Dyum Atub</name>
<state>zombie</state>
<kills>0</kills>
<fed>2010-09-20 00:32:00</fed>
<name id="14">Serild Phiser</name>
<state>zombie</state>
<kills>1</kills>
<fed>2010-09-19 18:49:33</fed>
<name id="15">Turoldi Osbald</name>
<state>zombie</state>
<kills>1</kills>
<fed>2010-09-18 21:35:44</fed>
<name id="16">Sumos Pervesi</name>
<state>zombie</state>
<kills>1</kills>
<fed>2010-09-17 16:18:46</fed>
<name id="17">Yerrotho Rispola</name>
<state>zombie</state>
<kills>3</kills>
<fed>2010-09-17 04:02:00</fed>
<name id="18">Quevhon Turdra</name>
<state>zombie</state>
<kills>0</kills>
<fed>2010-09-18 12:00:00</fed>
<name id="19">Ineagea Enhund</name>
<state>zombie</state>
<kills>0</kills>
<fed>2010-09-17 04:02:00</fed>
<name id="20">Vukim Iroughi</name>
<state>zombie</state>
<kills>0</kills>
<fed>2010-09-18 12:00:00</fed>
</players>
Here is my current processing code:
void setup() {
size(200, 200);
XMLElement xml;
xml = new XMLElement(this, "table.xml");
int numPlayers = xml.getChildCount();
int numZombies = xml.getChildCount(zombie);
for (int i = 0; i < numPlayers; i++) {
XMLElement kid = xml.getChild(i);
int id = kid.getIntAttribute("id");
String name = kid.getContent();
String state = kid.getContent();
}
}
void draw(){
float players = numPlayers/4;
float zombies = 2;
smooth();
background(255);
fill(255);
ellipse(100,100,150,150);
int diameter = 150;
float[] angs = {zombies/players*100};
float lastAng = 0;
for (int i = 0; i < angs.length; i++){
fill(255,0,0);
arc(width/2, height/2, diameter, diameter, lastAng, lastAng+radians(angs[i]*3.6));
lastAng += radians(angs[i]*3.6);
}
fill(255);
ellipse(100,100,100,100);
}
1