import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public static Document getXmlData(){ Document document = DocumentHelper.createDocument(); //生成一个结点 Element root = document.addElement("resinfo"); root.addAttribute("ver", "1.0"); root.addAttribute("encoding", "UTF-8"); //生成root的一个结点 Element resid = root.addElement("resid"); resid.addAttribute("caption","作品编号").addText("102102102"); Element filename = root.addElement("filename"); filename.addAttribute("caption","文件名称").addText("文件名称"); Element support_documents = root.addElement("support_documents"); support_documents.addAttribute("caption","其它文件"); Element filename1 = support_documents.addElement("filename"); filename1.addAttribute("caption","文件名称"); Element title = root.addElement("title"); title.addAttribute("caption","资源名称"); title.addComment("注释 注释"); return document; } public static void writeDocument(Document document, String outFile){ try{ //读取文件 //FileWriter fileWriter = new FileWriter(outFile); FileOutputStream fileWriter = new FileOutputStream(outFile); //如果有中文,用这个 //设置文件编码 OutputFormat xmlFormat = OutputFormat.createPrettyPrint(); //格式化输出 xmlFormat.setEncoding("UTF-8"); //创建写文件方法 XMLWriter xmlWriter = new XMLWriter(fileWriter,xmlFormat); //写入文件 xmlWriter.write(document); //关闭 xmlWriter.close(); }catch(IOException e){ System.out.println("文件没有找到"); e.printStackTrace(); } } //最后在主函数中调用 mineInfoManager.writeDocument(mineInfoManager.getXmlData(), pathTest + "LFMY/mineInfo/FusionCharts/Data/dataxml.xml");