How to create an attribute to an existing XML child?

Hi, I try to explore how to control/edit XML data, but it doesn't go well... One of the questions is that how you would create an attribute to an existing XML child?

It seems you can use setString to change an existing attribute but if I want to add an attribute, what should I do?

Thanks!

Answers

  • edited January 2017 Answer ✓

    XML xml = loadXML("mammals.xml");
    
    XML firstChild = xml.getChild("animal");
    println(firstChild);
    
    firstChild.setString("species", "I was modified");
    println(firstChild);
    
    firstChild.setString("newAttrib", "I'm brand new");
    println(firstChild);
    
    exit();
    

    <mammals>
      <animal id="0" species="Capra hircus">Goat</animal>
      <animal id="1" species="Panthera pardus">Leopard</animal>
      <animal id="2" species="Equus zebra">Zebra</animal>
    </mammals>
    

Sign In or Register to comment.