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

digester解析XML的两种方法

 
阅读更多

1、xml格式如下,多个<bill>标签

<?xml version="1.0" encoding="GBK"?>
<sell>
	<bill ss="1">						
		<sn>2010E01234</sn>				
		<bt>A123456789,B123456789</bt>	
		<oc>abc123</oc>					
		<on>方鑫利群专卖</on>			

		<pc>20001</pc>                  <!-- //买方编号 -->
		<pn>测试用户</pn>				<!-- //买方名称 -->
		<pd>1234567890ABCDE</pd>        <!-- //买方卡号 -->
		<ts>7</ts>						<!-- //总数量 -->
		<tz>5.000</tz>					<!-- //总重量 -->
		<ta>75.00</ta>					<!-- //总金额 -->
		
		<sd>2010-08-20 12:10:10</sd>	<!-- 销售时间 -->
		<item>
			<cn>白条一级</cn>				<!-- //品名 -->
			<ct>3</ct>						<!-- 数量 -->
			<tw>2.000</tw>					<!-- 重量 -->
			<tp>15.50</tp>					<!-- 单价 -->
			<tt>30.00</tt>					<!-- 金额 -->
			<bn>222,333</bn>				<!-- 酮体编号 -->
		</item>
		<item>
			<cn>白条特级</cn>				<!-- //品名 -->
			<ct>5</ct>						<!-- 数量 -->
			<tw>3.000</tw>					<!-- 重量 -->
			<tp>15.50</tp>					<!-- 单价 -->
			<tt>45.00</tt>					<!-- 金额 -->
			<bn>111,332</bn>				<!-- 酮体编号 -->
		</item>
		...
	</bill>
	
	...
</sell>

 

2、采用 digester解析,这里介绍有两种方法

 

完整代码如下

package org.mcp.data.logic;

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 java.util.ArrayList;
import java.util.List;

import org.apache.commons.digester.Digester;
import org.mcp.data.entity.Bill;
import org.mcp.data.entity.BillItem;
import org.mcp.util.DateConverterPlugin;
import org.mcp.util.DateHelper;
import org.mcp.util.EmptyStringUtil;


public class BillDigest {
	private Bill bill;
	private List<Bill> billList;

	public Bill getBill() {
		if(bill ==null)
			bill = new Bill();
		return bill;
	}
	
	private void initBill(){
		if(bill==null){
			bill = new Bill();
		}
	}
	
	public List<Bill> getBillList(){
		return billList;
	}
	
	private void initBillList(){
		if(billList ==null){
			billList = new ArrayList<Bill>();
		}
	}
	
	public void addBillList(
			String serialNo, 
			String incoming,
			String termNo, 
			String saleId, 
			String saleName, 
			String buyId, 
			String buyName,
			String buyCardNo,
			String totalWeight, 
			String totalMoney, 
			String saleDate, 
			String status,
			String userId,
			String meatTypeID) {
		initBillList();
		bill = this.getBill();
		bill.setSerialNo(EmptyStringUtil.stringToEmpty(serialNo));
		bill.setIncoming(EmptyStringUtil.stringToEmpty(incoming));
		bill.setTermNo(EmptyStringUtil.stringToEmpty(termNo));
		bill.getSupplier().setOrgNo(EmptyStringUtil.stringToEmpty(saleId));
		//bill.getSaleName().setOrgName(EmptyStringUtil.stringToEmpty(saleName));
		bill.getBuyer().setOrgNo(EmptyStringUtil.stringToEmpty(buyId));
		//bill.getBuyName().setOrgName(EmptyStringUtil.stringToEmpty(buyName));
		bill.setBuyCardNo(EmptyStringUtil.stringToEmpty(buyCardNo));
		bill.setMeatTypeID(EmptyStringUtil.integerToEmpty(meatTypeID));

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

	public static List<Bill> getBillList(String xml) {
		String parseXML = xml;
		
		List<Bill> billList = null;
		BillDigest helper;

		Digester digester = new Digester();
		digester.setValidating(false);
		
		digester.addObjectCreate("document", "org.mcp.data.logic.BillDigest");
		digester.addCallMethod("document/bill", "addBillList", 14);
		digester.addCallParam("document/bill/a", 0);
		digester.addCallParam("document/bill/b", 1);
		digester.addCallParam("document/bill/c", 2);
		digester.addCallParam("document/bill/d", 3);
		digester.addCallParam("document/bill/e", 4);
		digester.addCallParam("document/bill/f", 5);
		digester.addCallParam("document/bill/g", 6);
		digester.addCallParam("document/bill/h", 7);
		digester.addCallParam("document/bill/i", 8);
		digester.addCallParam("document/bill/j", 9);
		digester.addCallParam("document/bill/k", 10);
		digester.addCallParam("document/bill/l", 11);
		digester.addCallParam("document/bill/m", 12);
		digester.addCallParam("document/bill/n", 13);
		
		digester.addCallMethod("document/bill/billItem", "addBillItem", 5);
		digester.addCallParam("document/bill/billItem/ia", 0);
		digester.addCallParam("document/bill/billItem/ib", 1);
		digester.addCallParam("document/bill/billItem/ic", 2);
		digester.addCallParam("document/bill/billItem/id", 3);
		digester.addCallParam("document/bill/billItem/ie", 4);
		
		try {
			helper = (BillDigest)digester.parse(new StringReader(parseXML));
			billList = helper.getBillList();
		}catch (Exception e) {
			e.printStackTrace();
			billList = null;
		}
		return billList;
	}
	
		
	public void addBill(Bill bill) {
		initBillList();
				bill.setTriger(8);
		billList.add(bill);
	}
	
	public static List<Bill> getBillListRZ(String xml) {
		List<Bill> billList = null;
		BillDigest helper;
		
		Digester digester = new Digester();
        digester.setValidating(false);
// 指明匹配模式和要创建的类          
        digester.addObjectCreate("sell", BillDigest.class );
        digester.addObjectCreate("sell/bill", Bill.class );
        digester.addSetProperties("sell/bill", "ss", "billType");
// 设置对象属性,与xml文件对应        
        digester.addBeanPropertySetter("sell/bill/sn", "serialNo");
        digester.addBeanPropertySetter("sell/bill/sd", "saleDate");
        digester.addBeanPropertySetter("sell/bill/bt", "incoming");
        digester.addBeanPropertySetter("sell/bill/oc", "supId");
        digester.addBeanPropertySetter("sell/bill/on", "supName");
        
        digester.addBeanPropertySetter("sell/bill/pc", "buyId");
        digester.addBeanPropertySetter("sell/bill/pn", "buyName");
        digester.addBeanPropertySetter("sell/bill/pd", "buyCardNo");
        
        digester.addBeanPropertySetter("sell/bill/ts", "totalCount");
        digester.addBeanPropertySetter("sell/bill/tz", "totalWeight");
        digester.addBeanPropertySetter("sell/bill/ta", "totalMoney");
        
        digester.addObjectCreate("sell/bill/item", BillItem.class );
        digester.addBeanPropertySetter("sell/bill/item/cn", "variety");
        digester.addBeanPropertySetter("sell/bill/item/tw", "weight");
        digester.addBeanPropertySetter("sell/bill/item/tp", "price");
        digester.addBeanPropertySetter("sell/bill/item/tt", "total");
        
        digester.addBeanPropertySetter("sell/bill/item/bn", "code");
// 当移动到下一个标签中时的动作        
        digester.addSetNext("sell/bill/item", "addItem");
        digester.addSetNext("sell/bill", "addBill");

        try {
			helper = (BillDigest)digester.parse(new StringReader(xml));
			billList = helper.getBillList();
		}catch (Exception e) {
			e.printStackTrace();
			billList = null;
		}
		return billList;
	}
}

 

 

(1)方法 getBillList(String xml) 为第一种方式

    使用addCallMethod方法,映射XML所对应的节点与这个Call method的参数即可,这个call method将读取的一个标签内所有的值存入对象,然后再存入一个集合中.

 

(2)方法 getBillListRZ(String xml) 为第二种方式

    使用addObjectCreate方法,创建对象映射XML文件的属性对java Bean. 读取完一个对象将对象加入到一个集合中,然后再读取XML文件的下一个标签

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics