Tôi đã tạo một số mã viết bản đồ cho XML. Dường như nó hoạt động nhưng tệp được in không có dòng mới. Vì vậy, trong bất kỳ trình soạn thảo XML nào chỉ có trên một dòng. Làm thế nào tôi có thể in ra một dòng mới cho mỗi đứa trẻ?Tạo XML chỉ in thành một dòng
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element vdata = doc.createElement("vouchdata");
doc.appendChild(vdata);
for (Entry<String, String> entry : vouchMap.entrySet()) {
Element udata = doc.createElement("vouch");
Attr vouchee = doc.createAttribute("name");
vouchee.setValue(entry.getKey());
udata.setAttributeNode(vouchee);
Attr voucher = doc.createAttribute("vouchedBy");
voucher.setValue(entry.getValue());
udata.setAttributeNode(voucher);
vdata.appendChild(udata);
}
// 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("vouchdata.xml"));
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
Xem http://stackoverflow.com/questions/1264849/pretty-printing-output-from-javax-xml-transform -transformer-with-only-standard-j –