xpath 解析一

来源:互联网 发布:知乎三里屯不知道诊所 编辑:程序博客网 时间:2024/06/10 00:41

XPathMassReader:

package com.xpath;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.Node;

import com.mq.dto.TradeXMLMapper;
import com.mq.xml.XPath;
import com.hsbc.gbm.dml.mq.xml.XPathFactory;

public class XPathMassReader3 {
private String suffix = “”;

public static void main(String[] args){    new XPathMassReader2(args[0],args[1]);}public XPathMassReader3(String filename,String directory){    long start = System.currentTimeMillis();    File dir = new File(directory);    FileOutputStream output = null;    String fileName = (filename);    XSSFWorkbook myWorkBook = new XSSFWorkbook();    XSSFSheet mySheet = null;    XSSFRow myRow = null;    XSSFCell myCell = null;    int offset = 2;    if(dir.isDirectory()){        File[] xmlfiles = dir.listFiles();        ArrayList startList = new ArrayList();          int cells = startList.size() + 1 ;        int rows = xmlfiles.length + 1;        String[][] excelData = new String[rows] [cells];        int header=0;        while(header<startList.size()){            excelData[0][header+1] = ((TradeXMLMapper)startList.get(header)).getName();            header++;        }        int xmlsize = xmlfiles.length;        for(int i=0;i<xmlsize;i++){            if(xmlfiles[i].getName().toLowerCase().endsWith(suffix)&& xmlfiles[i].isFile()){                try{                if(output==null){                    output = new FileOutputStream(fileName);                }                String text = FileUtils.readFileToString(xmlfiles[i], "UTF-8");                XPath xpath = XPathFactory.createXPath(text);                System.out.println("Evaluating.. " + xmlfiles[i].getAbsolutePath());                excelData[i+1][0] = xmlfiles[i].getName();                int x=0;                for(;x<startList.size();x++){

// excelData[i+1][x+1] = xpath.stringValueOf(((TradeXMLMapper)startList.get(x)).getValue());
String val = ((TradeXMLMapper)startList.get(x)).getValue();
Node node = xpath.searchSingleNode(val);
excelData[i+1][x+1] = node.getTextContent();
// String valx = xpath.stringValueOf(((TradeXMLMapper)startList.get(x)).getValue());
// xpath.searchSingleNode()
}

                }catch(FileNotFoundException fnfe){                    fnfe.printStackTrace();                }catch(IOException io){                    io.printStackTrace();                }        }    }        mySheet=myWorkBook.createSheet();        for (int rowNum = 0; rowNum < rows; rowNum++){            myRow = mySheet.createRow(rowNum);            for (int cellNum = 0; cellNum < cells ; cellNum++){                myCell = myRow.createCell(cellNum);                myCell.setCellValue(excelData[rowNum][cellNum]);            }        }        try{                          myWorkBook.write(output);            output.close();            long end = System.currentTimeMillis();            System.out.println("Total elapsed time " + (end-start));        }catch(Exception e){ e.printStackTrace();}   }}

}

XPath:
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public interface XPath {

public abstract NodeList searchNodes2(String expression);public NodeList searchNodes(String expression) throws XPathException;public NodeList searchNodes(String expression, Object ref) throws XPathException;public Node searchSingleNode(String expression) throws XPathException;public Node searchSingleNode(String expression, Object ref) throws XPathException;public String stringValueOf(String expression) throws XPathException;public String stringValueOf(String expression, Object ref) throws XPathException;public boolean booleanValueOf(String expression) throws XPathException;public boolean booleanValueOf(String expression, Object ref) throws XPathException;public Document getDocument();public static interface Variable {    }    public static class XPathVariable implements Variable {    private String xpathExpression;    public XPathVariable(String xpathExpression) {        this.xpathExpression = xpathExpression;    }    public String getXPathExpression() {        return this.xpathExpression;    }}public static class HeaderVariable implements Variable {    private String header;    public HeaderVariable(String header) {        this.header = header;    }    public String getHeader() {        return this.header;    }}

}

0 0