我对如何将 xml 数据 append 到已存在的数据感到非常困惑,给我你的建议我的代码是这样的
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
我也可以轻松地解析和更新,但我不明白如何 append 以前的数据请帮助我
最佳答案
这很容易。说,你想追加新的 Employees
到您的 XML。您无需创建新根,只需使用 getElementsByName()
即可找到它喜欢
// 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()
和往常一样。
编辑:(添加示例代码)
你不能有两个根节点,即两个 <Employees>
标记为根。那是无效的 XML。你需要的是多个<Employee>
一个根内的标签 <Employees>
标签。另外,坚持使用 Camel 或大写字母。我使用大写字母是为了保持一致性。
// 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;
}
关于java - 如何在不覆盖现有数据的情况下使用 DOM append 现有 XML 文件?在 java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17249758/