java 生成xml 编码 utf-f 、 gb2312
没有难度,直接上代码了
/** * * @param fileName */ public static void createXmlUtf_8(String fileName) { Element root = DocumentHelper.createElement("root"); Document document = DocumentHelper.createDocument(root); Element title = root.addElement("title"); title.setText("标题");// 赋值 try {// 写入文件 FileWriter fileWriter = new FileWriter(fileName); OutputFormat format = new OutputFormat(); XMLWriter xmlWriter = new XMLWriter(fileWriter, format); xmlWriter.write(document); // 写入文件中 xmlWriter.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } /** * * @param fileName */ public static void createXmlGb2312(String fileName) { Element root = DocumentHelper.createElement("root"); Document document = DocumentHelper.createDocument(root); document.setXMLEncoding("gb2312"); Element title = root.addElement("title"); title.setText("标题");// 赋值 try {// 写入文件 java.io.Writer wr=new java.io.OutputStreamWriter(new java.io.FileOutputStream(fileName),"gb2312"); document.write(wr); wr.close(); } catch (IOException e) { System.out.println(e.getMessage()); } }
结果:
<?xml version="1.0" encoding="UTF-8" ?> <root> <title>标题</title> </root>
<?xml version="1.0" encoding="gb2312" ?> <root> <title>标题</title> </root>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。