public static void saveDATtoFile(Datafile datafile, String path)
throws ParserConfigurationException, JAXBException, TransformerException, FileNotFoundException {
JAXBContext jc = JAXBContext.newInstance(Datafile.class);
// Create the Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
DocumentType docType = getDocumentType(document);
// Marshal the Object to a Document formatted so human readable
Marshaller marshaller = jc.createMarshaller();
/*
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, docType.getSystemId());
*/
marshaller.marshal(datafile, document);
document.setXmlStandalone(true);
// NOTE could output with marshaller but cannot set DTD document type
/*
OutputStream os = new FileOutputStream(path);
marshaller.marshal(datafile, os);
*/
// Output the Document
TransformerFactory transformerFactory = TransformerFactory.newInstance();
// transformerFactory.setAttribute("indent-number", "4");
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
// transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "8");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, docType.getPublicId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docType.getSystemId());
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(path));
transformer.transform(source, result);
}
PersistUtils.java 文件源码
java
阅读 25
收藏 0
点赞 0
评论 0
项目:MFM
作者:
评论列表
文章目录