我想创建一个xml文件,并且已经看过这个示例(https://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/),但是我想在循环方法中生成它。
<company>
<staff id="1">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff id="2">
<firstname>hif</firstname>
<lastname>kiuk kim</lastname>
<nickname>gonuy</nickname>
<salary>50000</salary>
</staff>
</company>
但是,使用代码:
public void printXml(String firstName, String lastName, String nickname, String salary) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("company");
doc.appendChild(rootElement);
// staff elements
Element staff = doc.createElement("Staff");
rootElement.appendChild(staff);
// set attribute to staff element
Attr attr = doc.createAttribute("id");
attr.setValue("1");
staff.setAttributeNode(attr);
// shorten way
// staff.setAttribute("id", "1");
// firstname elements
Element firstnameElement = doc.createElement("firstname");
firstnameElement.appendChild(doc.createTextNode(firstName));
staff.appendChild(firstnameElement);
// lastname elements
Element lastnameElement = doc.createElement("lastname");
lastnameElement.appendChild(doc.createTextNode(lastName));
staff.appendChild(lastnameElement);
// nickname elements
Element nicknameElement = doc.createElement("nickname");
nicknameElement.appendChild(doc.createTextNode(nickname));
staff.appendChild(nicknameElement);
// salary elements
Element salaryElement = doc.createElement("salary");
salaryElement.appendChild(doc.createTextNode(salary));
staff.appendChild(salaryElement);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\\file.xml"));
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("File saved!");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
有关公司员工的循环信息总是被替换,并且仅保存最后的信息:
for (Staff staff: staffList){
String firstName = staff.getFirstName();
String lastName = staff.getLastName();
String nickname = staff.getNickname ();
double salary = staff.getSalary();
printXml(firstName, lastName, nickname, salary);
<company>
<staff id="2">
<firstname>hif</firstname>
<lastname>kiuk kim</lastname>
<nickname>gonuy</nickname>
<salary>50000</salary>
</staff>
</company>
如何保存循环中得到的所有信息?谢谢。
答案 0 :(得分:0)
您会覆盖文件,而不是附加到文件中。
您应该使用可以写入文件或追加到文件的流来初始化StreamResult
。
例如,您可以使用FileOutputStream
,并将true
传递为其构造函数以进行追加。
但是,您需要close
流,因此一种选择是更改代码,以使printXml
变成getXml
,然后您的代码如下所示:>
FileOutputStream stream = new FileOutputStream("C:\\file.xml", true);
StreamResult result = new StreamResult(stream);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
for (Staff staff: staffList){
DOMSource source = getXml(staff); // call staff's getters inside getXml
transformer.transform(source, result);
}
stream.close();
(您还应该将上面的3-4行移动到名为getTransformer
的方法上。)