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

来源:互联网 发布:预防网络诈骗班会流程 编辑:程序博客网 时间:2024/06/12 21:39
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");    }  } 
原创粉丝点击