Dom4j中String 、Document 、文件类型的xml之间的相互转换
1、通过xml String创建Document
?????? Document ttdocument = DocumentHelper.parseText(xml);
2、将Document 的xml格式化输出为文件
?
????? OutputFormat format = OutputFormat.createPrettyPrint();
??????? format.setEncoding("UTF-8");??
??????? //关键解决这个问就在此?? 把原来的FileWriter换成FileOutputStream
??????? XMLWriter write = new XMLWriter(new FileOutputStream("D:\\关联图xml.xml"),format);
??????? write.write(ttdocument);
??????? write.close();
?
3、通过文件创建Document?
??????? SAXReader saxReader = new SAXReader();
??????? Document tempdocument = saxReader.read(new File("D:\\xml.xml"));
?
4、Document?转换成String
??????? xml = tempdocument.asXML();