Using java to attach nodes to existing XML files
•
Java
Hello, I'm looking for a solution to attach nodes in Java to existing XML files
<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 want to add is a person node whose element is in the people element My big problem is that the data node is the root node If it is the person node as root, I can solve it But I can't get the personnel node under the personnel 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 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);
Solution
Without a library, you can do this:
Element dataTag = doc.getDocumentElement();
Element peopleTag = (Element) dataTag.getElementsByTagName("people").item(0);
Element newPerson = doc.createElement("person");
Element firstName = doc.createElement("firstName");
firstName.setTextContent("Tom");
Element lastName = doc.createElement("lastName");
lastName.setTextContent("Hanks");
newPerson.appendChild(firstName);
newPerson.appendChild(lastName);
peopleTag.appendChild(newPerson);
The results are as follows:
...
<person>
<firstName>Thomas</firstName>
<lastName>Tester</lastName>
<access>false</access>
<images>
<img>tt001.jpg</img>
</images>
</person>
<person>
<firstName>Tom</firstName>
<lastName>Hanks</lastName>
</person>
</people>
</data>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
