You should start with the XML reference and examples such as LoadSaveXML/XMLYahooWeather under Examples > Topics > Advanced Data
You can do a basic traversal through your nodes by calling getChilren() which returns an XML array. At this stage it should be simple enough to loop through this array and access data from each child XML instance at this level:
XML xml;
void setup(){
xml = loadXML("data.xml");
xml.trim();
XML[] level0 = xml.getChildren();
for(int i = 0; i < level0.length; i++){
println("level0["+i+"]:" + level0[i]);
}
}
Note1: The above example works with Processing 3, but may not work with older versions as the XML API changes (for example the trim() function might be missing). The trim() function should remove white spaces(new lines, tabs, etc.) otherwise, empty lines might be considered XML nodes. Always check to avoid surprises because due to white space in XML.
Note2 The examples in this answers assume the xml you posted is saved as data.xml in the sketch folder you're trying to run the code in. Feel free to rename that to what your XML file is actually called
Ok, so now you go through the first level, but how do you go to the next one ? It's actually not that bad. As you loop through each child XML node you can check if it hasChildren(), in which case, you repeat the prior operations:
- get the children xml nodes
- loop through each child xml node
(At this stage this would be 'grand' children:
XML xml;
void setup(){
xml = loadXML("data.xml");
xml.trim();
XML[] level0 = xml.getChildren();
for(int i = 0; i < level0.length; i++){
println("level0["+i+"]:" + level0[i]);
//we need to go deeper...but can we ?
if(level0[i].hasChildren()){
XML[] level1 = level0[i].getChildren();
for(int j = 0; j < level1.length; j++){
println("\tlevel1["+j+"]:" + level1[j]);
}
}
}
}
You should be able to safely repeat the process to drill down further.
As you go deeper and deeper there's something fun to play with: recursive functions. If you're not familiar with writing your own functions from scratch yet, you should first get a hang of that.
They've got a few things in common with variables:
- functions also have a type (called the return type)
- functions also have a name
But what do mainly do is group a set of instructions.
Depending on what these instructions do, they may or may not return a result, which determines the functions type.
The simplest are functions that don't return anything (therefore the return type is void). You've probably used void setup(){}
before, right ? :)
Functions create a separate scope(within the {}
symbols) so variables defined within the scope of the function will only be visible there, unless a resulting variable is returned at the end.
Function can have 0 or more arguments/parameters (see them as options).
For example setup()
needs no parameters, but point()
needs two (x and y).
A recursive function is essentially just a function that calls itself (usually with different parameters).
In the case of your XML, you can use a recursive function to drill down through all levels:
XML xml;
void setup(){
xml = loadXML("data.xml");
xml.trim();
recursiveTraverse(xml);
}
void recursiveTraverse(XML xml){
XML[] childNodes = xml.getChildren();
for(int i = 0; i < childNodes.length; i++){
println(childNodes[i].getName()+":"+childNodes[i].toString());
if(childNodes[i].hasChildren()){
recursiveTraverse(childNodes[i]);
}
}
}