I am trying to read a certain value in an XML file, I but fail and fail again

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. SCREEN SCREEN-2

Answers

  • edited July 2015

    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)

  • edited July 2015

    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...

  • edited July 2015

    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.

    XML xml;
    xml = loadXML("bubbles.xml");
    XML[] firstChild = xml.getChildren("bubble"); 
    
    
       // Looks at position 1
       XML secondChild = firstChild[1].getChild("diameter"); // Stores value into secondChild from specific part of array;  "diameter", using getChild,
       println(secondChild.getContent());                    // gets and prints content of  secondChild
    
       // Looks at position 0   
       XML thirdChild = firstChild[0].getChild("label");     // Stores value into secondChild from specific part of array;  "diameter", using getChild,
       println(thirdChild.getContent());                    // gets and prints content of  secondChild
    
  • If you paste in the XML, highlight it and press the C (code) button it should work...

  • Answer ✓

    And there should be no need to change the XML:

    Sketch:

    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);
    
        String test = children[i].getChild("test").getContent();
            println("test: " + test + "\n");
      }
    }
    

    XML:

    <?xml version="1.0"?>
    <mammals>
       <animal id="0" species="Capra hircus">
         <test>Globe</test>
         Goat
       </animal>
       <animal id="1" species="Panthera pardus">
        <test>mountain</test>
        Leopard
        </animal>
       <animal id="2" species="Equus zebra">
       <test>tree</test>
       Zebra
       </animal>
     </mammals>
    

    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 :)

Sign In or Register to comment.