解析XML文件(二)----使用dom4j方式(一)

51 篇文章 0 订阅
4 篇文章 0 订阅

针对之前写过的一种解析XML文件的方式 解析XML文件(一)----使用XStream方式 . 现在尝试用第二种常用的方式进行解析XML文件,便于对比学习!

顺便分享下网友总结的这个篇文章 dom4j解析xml   分别介绍了读、写、修改三个例子!

首先需要下载对应的jar包,我们系统用的是dom4j-1.6.jar(已上次我的资源,供下载分享)。

对于上一篇文章中的 查询成功后返回的XML文件格式:

现在做如下处理:

/**
	 * 将查询结果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, 0, 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, 0, 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);
			}
		}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值