Dom4j处理大数据的工具类(直接可以使用)


import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.io.SAXReader;

import com.zzb3.exp.FileIsNotFindException;
/**
 * Dom4J 处理大数据并将其导出到另外一个文件的写法
 * @author 消魂钉
 *
 */
public class BigDataElementHandler implements ElementHandler {


    private String inputFilePath;   //需要转化的XML
    private String outputFilePath;  //需要导出的CSV软件
    private String[] filedNames; //字段名称
    private String delimiter = "";
    private String BZF = "【123】";


    private SAXReader reader; 
    public BigDataElementHandler(String inputFilePath,String outputFilePath,String[] filedNames,String delimiter) throws FileIsNotFindException {  
        // test.xml文件跟类放在同一目录下  
       if (StringUtils.isBlank(inputFilePath))
           throw new FileIsNotFindException("錯誤:要转化的XML路径不能为空!");
       if (StringUtils.isBlank(outputFilePath))
           throw new FileIsNotFindException("錯誤:转化后的路径不能为空");
       if (ArrayUtils.isEmpty(filedNames))
           throw new FileIsNotFindException("錯誤:字段不能为空");
       if (StringUtils.isBlank(delimiter))
           throw new FileIsNotFindException("錯誤:分隔标识符不能为空");
       File file = new File(outputFilePath);
       if (file.exists())file.delete();
       this.inputFilePath = inputFilePath;
       this.outputFilePath= outputFilePath;
       this.filedNames = filedNames;
       this.delimiter = delimiter;
        try {  
            File files = new File(this.inputFilePath);
            if (!files.exists()) {
                throw new FileIsNotFindException("錯誤:要转化的文件不存在");
            }
            reader = new SAXReader();  
            reader.setDefaultHandler(this);  
            reader.read(files);  
        } catch (DocumentException  e) {  
            e.printStackTrace();  
        }  
    }

    @Override
    public void onEnd(ElementPath arg0) {

    }

    @Override
    public void onStart(ElementPath arg0) {
        Element e = arg0.getCurrent(); //获得当前节点 
        if("row".equals(e.getName())){
                try {
                StringBuffer sb = new StringBuffer("");
                for (String attrName : filedNames) {
                     Attribute attribute = e.attribute(attrName);
                     String temp = "";
                     if (attribute!=null) {
                         temp = this.viText(attribute.getStringValue());
                     }
                    sb.append(temp+delimiter);
                }
                if (sb.length()!=0) {
                    sb.deleteCharAt(sb.length()-1);
                }
                FileUtils.writeStringToFile(new File(outputFilePath),sb.toString()+"\n",true);
//              count++;
//              if (count%100==0) {
//                  System.out.println("我在走"+count);
//              }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }//追加模式
        }
        e.detach(); //记得从内存中移去  
    }

    private String viText(String node){
        if (node!=null) {
            if(node.indexOf("@")!=-1){
                node = node.replaceAll(delimiter, BZF);
                return node;
            };
            return node;
        }return "";
    }

    public static void main(String[] args) throws FileIsNotFindException{
        long startTime=System.currentTimeMillis();// 获取开始时间        
        new MainDataElementHandler("c:/11111/A01.xml","c:/11111/A01.csv",new String[]{"A0000NEW","A0194","A15Z101","A14Z101","A1701","ZZXW","ZZXL","QRZXW","QRZXL","A0140","XGSJ","XGR","A0117A","A0104A","A0165","A0198","A0149","A0148","A0199","A0187A","A0196","A01K01","A0192","A0195","A0148C","A0184","A0163","A0160","A0144","A3927","A3921","A0141","A0134","A0117","A0114A","A0114","A0111A","A0111","A0107","A0104","A0102","A0101","A0180","A0000","A0192B","A0192A","A015A","A0128","QRZXLXX","QRZXWXX","ZZXWXX","ZZXLXX","A0191"},"@");  
        long endTime=System.currentTimeMillis();// 获取结束时间  
        System.out.println("程序运行时间: " + (endTime - startTime)/1000 +"m");
    }  


} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值