快速解析大xml报文

如何快速解析大的xml文件。

案例:

package grp.pb.branch.qinhuangdao.batchvoucher.impl;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlDemo {
    public static void main(String[] args) {
        try {
            // 创建SAXReader
            SAXReader reader = new SAXReader();
            /*Document document = reader.read(
                    new File("F:\\tipsFiles\\1521105905823\\wage.xml"));
            // 获取根节点
            Element root = document.getRootElement();
            // 查找指定节点名称QName的所有子节点elements
            List<Element> list = root.elements("WAGE");*/
            
           /* Document document = reader.read(
                    new File("F:\\tipsFiles\\1521105905823\\unitvsitem.xml"));
            // 获取根节点
            Element root = document.getRootElement();
            // 查找指定节点名称QName的所有子节点elements
            List<Element> list = root.elements("ITEM");*/
            
            Document document = reader.read(
                    new File("F:\\tipsFiles\\1521105905823\\unit.xml"));
            // 获取根节点
            Element root = document.getRootElement();
            // 查找指定节点名称QName的所有子节点elements
            List<Element> list = root.elements("UNIT");
            // 获取emp
            for (Element object : list) {
                System.out.println(object.getName());
                // System.out.println(object.attribute("department").getData());
                for (Element element : (List<Element>) object.elements()) {
                    System.out.print(((Element) element).getName() + ":");
                    System.out.print(element.getText() + " ");
                }
                System.out.println();
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

 

 

----------------------------------------------------------------------------------------------

 

 

 

       SAXReader reader = new SAXReader();
//        DocumentHelper.
        Document document = reader.read( new File(destDirStr+File.separator+"wage.xml"));
        Element root = document.getRootElement();// 得到根节点
        // 获取根节点
    	List<Element> nodes = root.elements("WAGE");

    	for(Element e : nodes){
			BatchPayRequest request = new BatchPayRequest();
			String admdiv_code = e.selectSingleNode("CANTONID").getText();//区划
			request.setAdmdiv_code(admdiv_code);
			String pay_amount = e.selectSingleNode("SFX").getText();//实发项
			request.setPay_amount(new BigDecimal(pay_amount));
			String payee_account_no = e.selectSingleNode("ACCOUNTNUM").getText();//收款账号
			request.setPayee_account_no(payee_account_no);
			String payee_account_name = e.selectSingleNode("NAME").getText();//姓名
			request.setPayee_account_name(payee_account_name);
			
			//给batchPayRequets付一些基础属性
			long id = IdGen.genNumId();
			request.setBatchpay_request_id(id);
			request.setBatchpay_request_code("gongzi_"+id);
			request.setVt_code("8207");
			request.setBill_type_id(71);
			request.setBiz_type_id(100230002);//TODO:这个可能不对
			request.setTrans_succ_flag(QHDConstant.BATCH_REQUEST_NOT_TRANS);//未交易
			
			requestList.add(request);
		}

 

posted @ 2018-03-19 09:47  纯丿乱  阅读(2750)  评论(0编辑  收藏  举报