针对之前写过的一种解析XML文件的方式 解析XML文件(一)----使用XStream方式 .现在尝试用第二种常用的方式进行解析XML文件,便于对比学习!
顺便分享下网友总结的这个篇文章 dom4j解析xml 分别介绍了读、写、修改三个例子!
首先需要下载对应的jar包,我们系统用的是dom4j-1.6.jar(已上次我的资源,供下载分享)。
现在做如下处理:
/** * 将查询结果List生成xml并用GZip压缩 * */ private void setZipResults(List list,QueryRequest request,QueryResponse response) { //1.创建Document对象 Document document = DocumentHelper.createDocument(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); double grossPremium=0.0; // 生成xml if(list!=null){ Element root = document.addElement("rlt"); //2.创建根节点 root.addElement("st").addText("1");//3.创建子节点st,并设置内容 Element InsuranceList=root.addElement("InsuranceList"); InsuranceList.addElement("Count").addText(list.size()+""); for(int i=0;i<list.size();i++){ TPublicPolicy policy = (TPublicPolicy)list.get(i); grossPremium=grossPremium+policy.getGrossPremium(); } InsuranceList.addElement("TotalPremium").addText(grossPremium+""); Element Details=InsuranceList.addElement("Details"); Iterator it = list.iterator(); while (it.hasNext()) {//4.循环生成重复节点 TPublicPolicy policy = (TPublicPolicy) it.next(); String tcSerialNo=policy.getOuterOrderId(); String updated=sdf.format(policy.getApplyDate()); String applicantName=policy.getRfInsname(); String policyNum=policy.getPolicyNo(); String status=policy.getStatus()+""; String planCode=policy.getClassesCode(); String premium=policy.getGrossPremium().toString(); Element Insurance = Details.addElement("Insurance"); //循环子节点6 if (!StringUtil.isNull(tcSerialNo)) { Insurance.addElement("tcSerialNo").addText(tcSerialNo); } else { Insurance.addElement("tcSerialNo"); } if (!StringUtil.isNull(updated)) { Insurance.addElement("updated").addText(updated); } else { Insurance.addElement("updated"); } if (!StringUtil.isNull(applicantName)) { Insurance.addElement("applicantName").addText(applicantName); } else { Insurance.addElement("applicantName"); } if (!StringUtil.isNull(policyNum)) { Insurance.addElement("policyNum").addText(policyNum); } else { Insurance.addElement("policyNum"); } if (!StringUtil.isNull(status)) { Insurance.addElement("status").addText(status); } else { Insurance.addElement("status"); } if (!StringUtil.isNull(planCode)) { Insurance.addElement("planCode").addText(planCode); } else { Insurance.addElement("planCode"); } if (!StringUtil.isNull(status)) { Insurance.addElement("premium").addText(premium); } else { Insurance.addElement("premium"); } } } //创建XML文件并用GZip压缩 String fileName =String.valueOf(DateUtil.getSysDateLong()) + ".xml"; String zipFileName=String.valueOf(DateUtil.getSysDateLong()) + ".xml.zip"; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8");//处理中文乱码问题 XMLWriter writer = null; GZIPOutputStream zipout = null; FileInputStream zipin = null; BufferedInputStream in = null; ByteArrayOutputStream out = null; try { // a.写xml文件 File file = new File(fileName); writer = new XMLWriter(new BufferedWriter(new FileWriter(file)),format); writer.write(document); writer.flush(); // b.用GZip压缩文件 zipin = new FileInputStream(fileName); File zip = new File(zipFileName); zipout = new GZIPOutputStream(new FileOutputStream(zip)); byte[] buffer = new byte[4096]; int bytes_read; while ((bytes_read = zipin.read(buffer)) != -1) { zipout.write(buffer,bytes_read); } zipout.flush(); zipout.finish(); //c.读ZIP到BYTE[] in = new BufferedInputStream(new FileInputStream(zipFileName)); out = new ByteArrayOutputStream(4096); buffer = new byte[4096]; bytes_read = 0; while ((bytes_read = in.read(buffer)) != -1) { out.write(buffer,bytes_read); } out.flush(); response.setSysMessage(new SysMessage(SoapCodeConstant.TC_102,"xxx查询接口","XML文件处理成功")); response.setInsuranceDetailZip(out.toByteArray()); //byte[] } catch (Exception e) { // TODO Auto-generated catch block response.setSysMessage(new SysMessage( SoapCodeConstant.TC_100,"服务器内部处理请求异常","XML文件处理失败")); log.error("xxx请求结果处理XML文件时失败",e); } finally { try { if(writer!=null)writer.close(); if(zipin!=null)zipin.close(); if(zipout!=null)zipout.close(); if(in!=null)in.close(); if(out!=null)out.close(); new File(fileName).delete(); new File(zipFileName).delete(); } catch (Exception e) { response.setSysMessage(new SysMessage( SoapCodeConstant.TC_100,"XML文件处理失败")); log.error("请求结果处理XML文件时失败",e); log.error("",e); } } }原文链接:https://www.f2er.com/xml/300283.html