Hi im looking for a solution to append nodes from java into an existing xml file. What i got is an xml file like this
<data>
<people>
<person>
<firstName>Frank</firstName>
<lastName>Erb</lastName>
<access>true</access>
<images>
<img>hm001.jpg</img>
</images>
</person>
<person>
<firstName>Hans</firstName>
<lastName>Mustermann</lastName>
<access>true</access>
<images>
<img>hm001.jpg</img>
</images>
</person>
<person>
<firstName>Thomas</firstName>
<lastName>Tester</lastName>
<access>false</access>
<images>
<img>tt001.jpg</img>
</images>
</person>
</people>
</data>
what i whant to add is a person node with its elements inside the people element. My big problem is the data node which is root node. If it would be the Person node as root I could solve it. But I can't manage to get the person nodes under the people node.
<person>
<firstName>Tom</firstName>
<lastName>Hanks</lastName>
<access>false</access>
<images>
<img>tt001.jpg</img>
</images>
</person>
thanks for your help!
my java code looks as far like this
Element root = document.getDocumentElement();
// Root Element
Element rootElement = document.getDocumentElement();
Collection<Server> svr = new ArrayList<Server>();
svr.add(new Server());
for (Server i : svr) {
// server elements
Element server = document.createElement("people");
rootElement.appendChild(server);
//rootElement.appendChild(server);
Element name = document.createElement("person");
server.appendChild(name);
Element firstName = document.createElement("firstName");
firstName.appendChild(document.createTextNode(i.getFirstName()));
server.appendChild(firstName);
name.appendChild(firstName);
Element port = document.createElement("lastName");
port.appendChild(document.createTextNode(i.getLastName()));
server.appendChild(port);
name.appendChild(port);
Element access = document.createElement("access");
access.appendChild(document.createTextNode(i.getAccess()));
server.appendChild(access);
name.appendChild(access);
String imageName = Main.randomImgNr+"";
Element images = document.createElement("images");
//images.appendChild(document.createTextNode(i.getAccess()));
Element img = document.createElement("img");
img.appendChild(document.createTextNode(imageName));//i.getImage()));
images.appendChild(img);
server.appendChild(images);
name.appendChild(images);
root.appendChild(server);