`
liuxi1024
  • 浏览: 384922 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用digester解析XML

阅读更多

1、bill.xml内容

<?xml version="1.0" encoding="GBK"?>
<document>
	<bill>
		<serialNo>20089220014</serialNo>                   <!-- 流水号 -->
		<termNo>N0795</termNo>                                  <!-- 机器号 -->
		<saleId>62</saleId>								    <!-- 售货单位备案登记编码 -->
		<saleName>发源</saleName>			<!-- 售货单位 -->

		<buyId>74</buyId>							        <!-- 购货单位备案登记编码 -->
		<buyName>基由</buyName>			  <!-- 购货单位 -->
		
		<totalWeight>40</totalWeight>                          <!-- 合计重量(公斤) -->
		<totalMoney>220</totalMoney>                           <!-- 合计金额(元)-->
		<saleDate>2008-8-18 00:00:00</saleDate>                 <!-- 交易时间 -->
		<status>0</status>                                      <!-- 是否作废 -1:作废 0/'':正常 -->
		
		<billItem>
			<variety>五花肉</variety>								<!-- 品 种 -->
			<weight>10</weight>									<!-- 重量(公斤) -->
			<price>5</price>									<!-- 单价(元/公斤) -->
			<total>50</total>									<!-- 金 额(元) -->
		</billItem>

		<billItem>
			<variety>纯瘦肉</variety>								<!-- 品 种 -->
			<weight>10</weight>									<!-- 重量(公斤) -->
			<price>6</price>									<!-- 单价(元/公斤) -->
			<total>60</total>									<!-- 金 额(元) -->
		</billItem>
		
		<billItem>
			<variety>猪蹄</variety>								<!-- 品 种 -->
			<weight>10</weight>									<!-- 重量(公斤) -->
			<price>5</price>									<!-- 单价(元/公斤) -->
			<total>50</total>									<!-- 金 额(元) -->
		</billItem>
		
		<billItem>
			<variety>猪耳朵</variety>								<!-- 品 种 -->
			<weight>10</weight>									<!-- 重量(公斤) -->
			<price>6</price>									<!-- 单价(元/公斤) -->
			<total>60</total>									<!-- 金 额(元) -->
		</billItem>
	</bill>
</document>

 2、解析xml 的类

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;

import org.apache.commons.digester.Digester;

import com.data.bill.entity.Bill;
import com.data.bill.entity.BillItem;
import com.util.DateHelper;


public class BillDigest {
	private Bill bill;

	public Bill getBill() {
		return bill;
	}

	private void initBill() {
		if (bill == null) {
			bill = new Bill();
		}
	}
	
	public void addBill(
			String serialNo, 
			String termNo, 
			String saleId, 
			String saleName, 
			String buyId, 
			String buyName,
			String totalWeight, 
			String totalMoney, 
			String saleDate, 
			String status) {
		initBill();
		bill.setSerialNo(stringToEmpty(serialNo));
		bill.setTermNo(stringToEmpty(termNo));
		bill.setSaleId(integerToEmpty(saleId));
		bill.setSaleName(stringToEmpty(saleName));
		bill.setBuyId(integerToEmpty(buyId));
		bill.setBuyName(stringToEmpty(buyName));

		bill.setTotalWeight(doubleToEmpty(totalWeight));
		bill.setTotalMoney(doubleToEmpty(totalMoney));
		bill.setSaleDate(DateHelper.parseDate(saleDate));
		bill.setStatus(integerToEmpty(status));
	}
	
	public void addBillItem(
			String variety, 
			String weight, 
			String price, 
			String total){
		initBill();
		BillItem item = new BillItem();
		item.setVariety(stringToEmpty(variety));
		item.setWeight(doubleToEmpty(weight));
		item.setPrice(doubleToEmpty(price));
		item.setTotal(doubleToEmpty(total));
		item.setHeader(bill);
		bill.getItems().add(item);
	}
	
	public static Bill getBill(File xmlFile) {
		BufferedReader reader = null;
		Bill bill = null;
		String s;
		StringBuffer buffer = new StringBuffer();
		try {
			reader = new BufferedReader(new FileReader(xmlFile));
			while((s = reader.readLine()) != null) {
				buffer.append(s);
			}
			bill = getBill(buffer.toString());
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
			bill = null;
		}
		return bill;
	}
	
	public static Bill getBill(InputStream is) {
		if (is == null) {
			return null;
		}
		
		BufferedReader reader = null;
		Bill bill = null;
		String s;
		StringBuffer buffer = new StringBuffer();
				
		try {
			reader = new BufferedReader(new InputStreamReader(is));
			while ((s = reader.readLine()) != null) {
				buffer.append(s);
			}
			bill = getBill(buffer.toString());
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
			bill = null;
		}
		
		return bill;
	}

	public static Bill getBill(String xml) {
		
		///StringBuffer buffer = new StringBuffer(xml);
		/*Pattern p = Pattern.compile("<[^>]*?>");
		Matcher m = p.matcher(buffer);
		while(m.find()) {
			buffer.replace(m.start(), m.end(), xml.substring(m.start(),m.end()).toLowerCase());
		}*/
		String parseXML = xml;
		
		Bill bill = null;
		BillDigest helper;
		//生成一个digester。主要需要引进commons-logging.jar、commons-collections.jar、commons-beanutils.jar
		Digester digester = new Digester();
		//设置对XML文档资料不进行DTD(Document type definition)验证
		digester.setValidating(false);
		//当遇见 document 元素的时候,产生一个BillDigest对象
		digester.addObjectCreate("document", BillDigest.class);
		//当遇见 document/bill 元素的时候,使用addBill方法进行赋值,有10个参数
		digester.addCallMethod("document/bill", "addBill", 10);
		//当遇见 document/bill/* 元素的时候,作为参数
		digester.addCallParam("document/bill/serialNo", 0);
		digester.addCallParam("document/bill/termNo", 1);
		digester.addCallParam("document/bill/saleId", 2);
		digester.addCallParam("document/bill/saleName", 3);
		digester.addCallParam("document/bill/buyId", 4);
		digester.addCallParam("document/bill/buyName", 5);
		digester.addCallParam("document/bill/totalWeight", 6);
		digester.addCallParam("document/bill/totalMoney", 7);
		digester.addCallParam("document/bill/saleDate", 8);
		digester.addCallParam("document/bill/status", 9);
		
		//当遇见 document/bill/billItem 元素的时候,使用addBillItem方法进行赋值,有4个参数
		digester.addCallMethod("document/bill/billItem", "addBillItem", 4);
		//当遇见 document/bill/billItem/* 元素的时候,作为参数
		digester.addCallParam("document/bill/billItem/variety", 0);
		digester.addCallParam("document/bill/billItem/weight", 1);
		digester.addCallParam("document/bill/billItem/price", 2);
		digester.addCallParam("document/bill/billItem/total", 3);
		
		try {
			//用digester解析指定的文件
			helper = (BillDigest)digester.parse(new StringReader(parseXML));
			bill = helper.getBill();
		}catch (Exception e) {
			e.printStackTrace();
			bill = null;
		}
		return bill;
	}
	
	public static String stringToEmpty(String obj)
	{
		if(obj==null){
			return "";
		}else{
			return obj;
		}
	}
	
	public static Integer integerToEmpty(String obj)
	{
		if(obj==null){
			return new Integer(0);
		}else{
			return Integer.valueOf(obj);
		}
	}
	
	public static Double doubleToEmpty(String obj)
	{
		if(obj==null){
			return new Double(0);
		}else{
			return Double.valueOf(obj);
		}
	}
}

 

 总结:通过调用

public static Bill getBill(File xmlFile) 、public static Bill getBill(InputStream is) 、public static Bill getBill(String xml) ,传入不同的参数,返回Bill对象。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics