当前位置: 技术问答>java相关
XML对象怎样写入一个XML文件?
来源: 互联网 发布时间:2017-03-30
本文导语: XML对象Document dataDoc从数据库中得到了很多数据库并且已经生成一个完整的Document,可以遍历它的所有Node和元素,但是不知道怎样把它保存为一个XML文件? 请各位大哥帮忙! | 用javax.xml.transform.Transformer ransforme...
XML对象Document dataDoc从数据库中得到了很多数据库并且已经生成一个完整的Document,可以遍历它的所有Node和元素,但是不知道怎样把它保存为一个XML文件?
请各位大哥帮忙!
|
用javax.xml.transform.Transformer
ransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
transformer.transform(new DOMSource(dom), new StreamResult(new FileOutputStream(filePath)));
ransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
transformer.transform(new DOMSource(dom), new StreamResult(new FileOutputStream(filePath)));