Java appending XML data

Posted by Travis on Stack Overflow See other posts from Stack Overflow or by Travis
Published on 2010-05-15T04:32:48Z Indexed on 2010/05/15 4:44 UTC
Read the original article Hit count: 299

Filed under:
|
|

I've already read through a few of the answers on this site but none of them worked for me. I have an XML file like this:

<root>
    <character>
        <name>Volstvok</name>
        <charID>(omitted)</charID>
        <userID>(omitted)</userID>
        <apiKey>(omitted)</apiKey>
    </character>
</root>

I need to add another <character> somehow.

I'm trying this but it does not work:

public void addCharacter(String name, int id, int userID, String apiKey){
    Element newCharacter = doc.createElement("character");

    Element newName = doc.createElement("name");
    newName.setTextContent(name);

    Element newID = doc.createElement("charID");
    newID.setTextContent(Integer.toString(id));

    Element newUserID = doc.createElement("userID");
    newUserID.setTextContent(Integer.toString(userID));

    Element newApiKey = doc.createElement("apiKey");
    newApiKey.setTextContent(apiKey);

    //Setup and write
    newCharacter.appendChild(newName);
    newCharacter.appendChild(newID);
    newCharacter.appendChild(newUserID);
    newCharacter.appendChild(newApiKey);
    doc.getDocumentElement().appendChild(newCharacter);
}

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml