直接来个简单的例子,大家一看便知。
1 Document doc=DocumentHelper.createDocument();//创建document 2 Element rootElement=doc.addElement("siteConfig");//添加根元素 3 rootElement.addAttribute("id", "0001"); 4 rootElement.setText("站点配置"); 5 rootElement.addComment("文档的根siteConfig已经创建。");//添加注释 6 Element secondElement0 = rootElement.addElement("标识"); 7 secondElement0.addAttribute("id", "010"); 8 secondElement0.addAttribute("name", "ID"); 9 secondElement0.setText(configSite.getId()); 10 Element secondElement1 = rootElement.addElement("名称"); 11 secondElement1.addAttribute("id", "011"); 12 secondElement1.addAttribute("name", "NAME"); 13 secondElement1.setText(configSite.getName()); 14 Element secondElement2 = rootElement.addElement("生成首页文件名称"); 15 secondElement2.addAttribute("id", "012"); 16 secondElement2.addAttribute("name", "VIEW_PATH_NAME"); 17 secondElement2.setText(configSite.getViewPathName()); 18 Element secondElement3 = rootElement.addElement("谁有角色IDS"); 19 secondElement3.addAttribute("id", "013"); 20 secondElement3.addAttribute("name", "ROLES"); 21 if(configSite.getRoles() == null){ 22 secondElement3.setText("null"); 23 }else{ 24 secondElement3.setText(configSite.getRoles()); 25 } 26 Element secondElement4 = rootElement.addElement("创建时间"); 27 secondElement4.addAttribute("id", "014"); 28 secondElement4.addAttribute("name", "CREATE_TIME"); 29 secondElement4.setText(configSite.getCreateTime().getTime().toString()); 30 Element secondElement5 = rootElement.addElement("创建人姓名"); 31 secondElement5.addAttribute("id", "015"); 32 secondElement5.addAttribute("name", "CREATE_NAME"); 33 secondElement5.setText(configSite.getCreateName()); 34 Element secondElement6 = rootElement.addElement("模板ID"); 35 secondElement6.addAttribute("id", "016"); 36 if(configSite.getSiteTemplate() == null){ 37 secondElement6.setText("null"); 38 }else{ 39 secondElement6.setText(configSite.getSiteTemplate().getId()); 40 Element thirdElement1 = secondElement6.addElement("模板名称"); 41 thirdElement1.addAttribute("id", "0161"); 42 thirdElement1.addAttribute("name", "NAME"); 43 thirdElement1.setText(configSite.getSiteTemplate().getName()); 44 Element thirdElement2 = secondElement6.addElement("模板图片名称"); 45 thirdElement2.addAttribute("id", "0162"); 46 thirdElement2.addAttribute("name", "PICTURE_NAME"); 47 thirdElement2.setText(configSite.getSiteTemplate().getPictureName()); 48 Element thirdElement3 = secondElement6.addElement("模板文件名称"); 49 thirdElement3.addAttribute("id", "0163"); 50 thirdElement3.addAttribute("name", "FILE_NAME"); 51 thirdElement3.setText(configSite.getSiteTemplate().getFileName()); 52 } 53 54 try { 55 /*自己查帮助文档 56 * OutputFormat format=new OutputFormat(" ",true,"gb2312"); 57 */ 58 /* 59 * 创建一个漂亮的打印格式的OutputFormat 60 * 可以通过setEncoding来设置其传输字符串,默认为utf-8 61 */ 62 OutputFormat format = OutputFormat.createPrettyPrint(); 63 format.setEncoding("utf8"); 64 /*创建缩进格式的OutputFormat 65 format = OutputFormat.createCompactFormat(); 66 */ 67 JFileChooser jf = new JFileChooser(); 68 jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 69 jf.showDialog(null,null); 70 File fi = jf.getSelectedFile(); 71 String f = fi.getAbsolutePath()+"\"+ configSite.getName() +".xml"; 72 XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File(f)), "UTF-8"), format); 73 writer.write(doc); 74 writer.flush(); 75 writer.close(); 76 } catch (IOException e) { 77 e.printStackTrace(); 78 }
个人比较偷懒,直接拿的自己做的项目中的代码来的,其中try之前的部分都是构造xml文件的内容,大家可以写简单的内容,不用太过复杂,或者截取我的一部分也行。比如:
Document doc=DocumentHelper.createDocument();//创建document Element rootElement=doc.addElement("root");//添加根元素 rootElement.addAttribute("id", "0001"); rootElement.setText("站点配置"); rootElement.addComment("文档的根siteConfig已经创建。");//添加注释 Element secondElement0 = rootElement.addElement("标识"); secondElement0.addAttribute("id", "010"); secondElement0.addAttribute("name", "ID"); secondElement0.setText(configSite.getId()); Element secondElement1 = rootElement.addElement("名称"); secondElement1.addAttribute("id", "011"); secondElement1.addAttribute("name", "NAME"); secondElement1.setText(configSite.getName()); Element secondElement2 = rootElement.addElement("生成首页文件名称"); secondElement2.addAttribute("id", "012"); secondElement2.addAttribute("name", "VIEW_PATH_NAME"); secondElement2.setText(configSite.getViewPathName());
后面的内容中,就是try之后的内容,包括了设置编码格式,用JFileChooser实现弹出选择地址框,通过jfilechooser获取文件路径,再通过xmlwriter导出。基本上就是这样。
我在网上找了好久,要么就是用JFileChooser导出的TXT,要么就是用Dom4j导出写死的xml文件的路径,我将两者结合了,实现了自己的功能。希望对大家有帮助。