We are about to switch to a new forum software. Until then we have removed the registration on this forum.
This is the code from were I start;
XML xml;
void setup() {
xml = loadXML("mammals.xml");
XML[] children = xml.getChildren("animal");
for (int i = 0; i < children.length; i++) {
int id = children[i].getInt("id");
String coloring = children[i].getString("species");
String name = children[i].getContent();
println(id + ", " + coloring + ", " + name);
}
}
but none of my adaptions give me access to the "test" element. I tried syntax like this;
for( int j = 0 ; j < 10 ; j++ ){
XML[] nameHelper = children[j].getChildren("description");
}
but this gives me some weird hex-like values when I try nameHelper[0], I get a null. when I try nameHelper[1], I get a null pointer exeception.
sorry if I am not clear in my explanation.
here are the screenshots from the xlm.
Answers
Don't double post!
<test>
is a child of animal. In your loop have you tried:String test = children[i].getChild("test");Edited: getChild("test") returns the node test and not the String content; hence why this didn't work. See working code below...
(duplicate deleted)
Sorry for the double post.
String test = children[i].getChild("test");
gives me
"cannot convert XML to String"...
while
String coloring = children[i].getString("species");
gives no error, probably because its passing a string anyway.
thought
String test = children[i].getString("test");
gives me a null ;( :-S
and
XML test = children[i].getChild("test");
also returns a null...
Finally I found the solution thought the structure of the XML is a bit different I want to post an image of said file to the forum but for some reason I cannot upload the thing (?) I will try later thought.
If you paste in the XML, highlight it and press the C (code) button it should work...
And there should be no need to change the XML:
Sketch:
XML:
geChild("test") returns the node 'test' and not the String content; so it's an extra function call to get to the content.
Working with XML in Java reminds me why I prefer JSON and JavaScript :)