问题描述
我正在寻找将节点从 java 附加到现有 xml 文件的解决方案.我得到的是一个像这样的 xml 文件
<人><人物><firstName>弗兰克</firstName><lastName>Erb</lastName><access>true</access><img>hm001.jpg</img></图像></人><人物><firstName>Hans</firstName><lastName>Mustermann</lastName><access>true</access><img>hm001.jpg</img></图像></人><人物><firstName>Thomas</firstName><lastName>测试员</lastName><access>false</access><img>tt001.jpg</img></图像></人></人></数据>
我想添加的是一个 person 节点,其元素位于 people 元素内.我的大问题是作为根节点的数据节点.如果它是作为 root 的 Person 节点,我可以解决它.但是我无法在people节点下获取person节点.
<firstName>Tom</firstName><lastName>汉克斯</lastName><access>false</access><img>tt001.jpg</img></图像></人>
感谢您的帮助!
我的java代码看起来像这样
Element root = document.getDocumentElement();//根元素元素 rootElement = document.getDocumentElement();集合svr = new ArrayList();svr.add(新服务器());对于(服务器我:svr){//服务器元素元素服务器 = document.createElement("people");rootElement.appendChild(server);//rootElement.appendChild(server);元素名称 = document.createElement("person");server.appendChild(name);Element firstName = document.createElement("firstName");firstName.appendChild(document.createTextNode(i.getFirstName()));server.appendChild(firstName);name.appendChild(firstName);元素端口 = document.createElement("lastName");port.appendChild(document.createTextNode(i.getLastName()));server.appendChild(port);name.appendChild(port);元素访问 = document.createElement("访问");access.appendChild(document.createTextNode(i.getAccess()));server.appendChild(访问);name.appendChild(access);String imageName = Main.randomImgNr+"";元素图像 = document.createElement("图像");//images.appendChild(document.createTextNode(i.getAccess()));元素 img = document.createElement("img");img.appendChild(document.createTextNode(imageName));//i.getImage()));images.appendChild(img);server.appendChild(images);name.appendChild(images);root.appendChild(服务器);
如果没有图书馆,你可以做这样的事情:
Element dataTag = doc.getDocumentElement();元素 peopleTag = (Element) dataTag.getElementsByTagName("people").item(0);元素 newPerson = doc.createElement("person");Element firstName = doc.createElement("firstName");firstName.setTextContent("Tom");元素 lastName = doc.createElement("lastName");lastName.setTextContent("汉克斯");newPerson.appendChild(firstName);newPerson.appendChild(lastName);peopleTag.appendChild(newPerson);
结果:
...<人物><firstName>Thomas</firstName><lastName>测试员</lastName><access>false</access><img>tt001.jpg</img></图像></人><人物><firstName>Tom</firstName><lastName>汉克斯</lastName></人></人></数据>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);
Without a library you can do something like 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);
Which results:
...
<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>
这篇关于使用 java 将节点附加到现有的 XML 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!