引言
概述
页面导出txt的原理基本与导出txt一致,只不过需要生成一个DOM文档,然后将DOM树再生成xml文件。
内容
实现思路:新建DOM树,将数据拼接到DOM树中,生成xml文件。
代码实例:
/* * 生成xml文件-2017年8月11日 13:49:45-李明 */ public void writeToXml(String fileName,List<FpSpbm> listFpSpBm) { try { // 创建Document Document document = DocumentHelper.createDocument(); // 创建根节点 Element business = document.addElement("business"); business.addAttribute("version","1.0"); business.addAttribute("comment","商品编码请求"); business.addAttribute("id","SPBM"); // 创建一级子节点,并设置属性 Element body = business.addElement("body"); //创建二级子节点,并设置属性 Element returnCode = body.addElement("returnCode"); returnCode.setText("00"); Element returnMessage = body.addElement("returnMessage"); returnMessage.setText("成功"); Element BBH = body.addElement("BBH"); BBH.setText("12.0"); Element COUNT = body.addElement("COUNT"); COUNT.setText(""+listFpSpBm.size()); Element MWJY = body.addElement("MWJY"); MWJY.setText("00000000"); //创建三级子节点 for(int i=0;i<listFpSpBm.size();i++) { Element BMXX = body.addElement("BMXX"); FpSpbm fpspbm = listFpSpBm.get(i); Element SPBM = BMXX.addElement("SPBM"); if(fpspbm.getBm()!=null) { SPBM.setText(fpspbm.getBm()); } Element PID = BMXX.addElement("PID"); if(fpspbm.getPid()!=null) { PID.setText(fpspbm.getPid()); } Element SCBM = BMXX.addElement("SCBM"); Element JM = BMXX.addElement("JM"); if(fpspbm.getJm()!=null) { JM.setText(fpspbm.getJm()); } Element SPMC = BMXX.addElement("SPMC"); if(fpspbm.getMc()!=null) { SPMC.setText(fpspbm.getMc()); } Element ZZSSL = BMXX.addElement("ZZSSL"); Element GGXH = BMXX.addElement("GGXH"); if(fpspbm.getGgxh()!=null) { GGXH.setText(fpspbm.getGgxh()); } Element JLDW = BMXX.addElement("JLDW"); if(fpspbm.getJldw()!=null) { JLDW.setText(fpspbm.getJldw()); } Element DJ = BMXX.addElement("DJ"); if(fpspbm.getDj()!=null) { DJ.setText(fpspbm.getDj().toString()); } Element HSBZ = BMXX.addElement("HSBZ"); if(fpspbm.getHsjbz()!=null) { HSBZ.setText(fpspbm.getHsjbz()); } Element CLLXMC = BMXX.addElement("CLLXMC"); Element CPXH = BMXX.addElement("CPXH"); Element SCQYMC = BMXX.addElement("SCQYMC"); Element CD = BMXX.addElement("CD"); Element SM = BMXX.addElement("SM"); if(fpspbm.getSm()!=null) { SM.setText(fpspbm.getSm()); } Element SLBZ = BMXX.addElement("SLBZ"); Element SYYHBZ = BMXX.addElement("SYYHBZ"); Element KYSL = BMXX.addElement("KYSL"); Element ZZSTSGL = BMXX.addElement("ZZSTSGL"); Element GJZ = BMXX.addElement("GJZ"); Element HZX = BMXX.addElement("HZX"); Element SYPC = BMXX.addElement("SYPC"); Element SYSJ = BMXX.addElement("SYSJ"); Element ZDYBZ = BMXX.addElement("ZDYBZ"); Element XYBZ = BMXX.addElement("XYBZ"); Element KYZT = BMXX.addElement("KYZT"); Element FPLX = BMXX.addElement("FPLX"); Element BB = BMXX.addElement("BB"); Element YH = BMXX.addElement("YH"); } OutputFormat format = OutputFormat.createPrettyPrint();// 创建文件输出的时候,自动缩进的格式 format.setEncoding("UTF-8");//设置编码 XMLWriter writer = new XMLWriter(new FileWriter(filepath + fileName),format); writer.write(document); writer.close(); } catch (Exception e) { e.printStackTrace(); } }
总结
虽然较之导出txt的原理一致,但是在这里需要注意新建dom文档的方法,网上资料新建dom文件一般有四种方法,个人建议使用dom4j这种方法,这种方法优点比较全面,也是最常用的方法。如果想要深入了解,请读者在网上查看资料。