How to append an existing XML file using DOM without overwriting the existing data? in java(如何使用 DOM 追加现有 XML 文件而不覆盖现有数据?在java中)
问题描述
我对如何将 xml 数据附加到已经存在的数据感到非常困惑,请给我您的建议,我的代码是这样的
i have a very confused about the how the xml data can be append to already existed data give me your suggestions my code is like this
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();//db.newDocument();//create document
Element root = doc.createElement("Employees");//cretae Elements
doc.appendChild(root);
Comment cmt = doc.createComment("Employee Details");//Add comment to xml
root.appendChild(cmt);
Element employee = doc.createElement("employee");//create Element
//employee.appendChild(doc.)
root.appendChild(employee);
Attr genderAttr = doc.createAttribute("Gender");
System.out.print("Enter your gender :");
String gend = br.readLine();
genderAttr.setValue(gend);
employee.setAttributeNode(genderAttr);
System.out.print("Enter first name:");
String child = br.readLine();
Element FName = doc.createElement("firstName");
FName.appendChild(doc.createTextNode(child));//set xml text
employee.appendChild(FName);
System.out.print("Enter last name:");
String child1 = br.readLine();
Element LName = doc.createElement("lastName");
LName.appendChild(doc.createTextNode(child1));
employee.appendChild(LName);
//root.appendChild(employee);
//doc.appendChild(root);
//to write on file/screen
TransformerFactory tf = TransformerFactory.newInstance();
Transformer tr = tf.newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);//source
//File shopOrder = new File("src"+File.separator+"xmlparsing"+File.separator+"xmlParse1.xml");//get the file
StreamResult res = new StreamResult(new File("src"+File.separator+"xmlparsing"+File.separator+"xmlParse1.xml"));//Destination
tr.transform(source, res);//to write on file
我也可以轻松解析和更新,但我不明白如何附加以前的数据,请帮助我
and i can do easily parse and update also but i can't understand how i can append previous data please help me
推荐答案
很简单.假设您想将新的 Employees
附加到您的 XML 中.无需创建新根,您只需使用 getElementsByName()
之类的
It's pretty easy. Say, you want to append new Employees
to your XML. Instead of creating a new root you'd simply find it using getElementsByName()
like
// find root
NodeList rootList = doc.getElementsByName("Employees");
Node root = rootList.item(0);
Element employee = doc.createElement("employee"); //create new Element
root.appendChild(employee); // append as before
还有一个 Document.getElementById()
方法,如果已为元素分配了标识符,您也可以使用该方法.要在树的深处插入一些东西,首先使用 XPath
查找节点,然后像往常一样使用 append()
.
There's a Document.getElementById()
method as well that you could use if an element has been assigned an identifier. To insert something deep down the tree use XPath
to find the node first then append()
as usual.
(添加示例代码)
您不能有两个根节点,即两个 <Employees>
标记作为根.那是无效的 XML.您需要的是一个根 <Employees>
标记内的多个 <Employee>
标记.此外,坚持骆驼或大写.我使用大写字母来保持一致性.
EDIT : (Sample code added)
You can't have two root nodes i.e. two <Employees>
tags as root. That's invalid XML. What you need is multiple <Employee>
tags inside one single root <Employees>
tag. Also, stick to either camel or capital case. I'm using capitals for consistency.
// find root
NodeList rootList = doc.getElementsByName("Employees");
Node root = rootList.item(0);
// append using a helper method
root.appendChild(createEmployee(doc, "male", "John", "Doe"));
public Element createEmployee(Document doc,
String gender, String fname, String lname) {
// create new Employee
Element employee = doc.createElement("Employee");
employee.setAttribute("gender", gender);
// create child nodes
Element firstName = doc.createElement("FirstName");
firstName.appendChild(doc.createTextNode(fname));
Element lastName = doc.createElement("LastName");
lastName.appendChild(doc.createTextNode(lname));
// append and return
employee.appendChild(firstName);
employee.appendChild(lastName);
return employee;
}
这篇关于如何使用 DOM 追加现有 XML 文件而不覆盖现有数据?在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 DOM 追加现有 XML 文件而不覆盖现有数据?在java中
基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01