java读取并解析oracle中blob类型XML数据

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class CopyOfTest {

    public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
    public static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    public static final String USERNAME = "XXX";
    public static final String PASSWORD = "XXX";

    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        if (download()) {
            System.out.println("下载成功");
        } else {
            System.out.println("下载失败");
        }
    }

    public static boolean download() {
        Connection conn = getConnection();
        Statement sta = null;
        ResultSet rs = null;
        try {
            sta = conn.createStatement();
            String sql = "Select name,  blob from XXX where ID='BE4A50CF7F000001287BFF71B5AEDF85'";
            rs = sta.executeQuery(sql);
            String newStr = "";
            while (rs.next()) {
                oracle.sql.BLOB blob = null;
                InputStream is = null;
                blob = (oracle.sql.BLOB) rs.getBlob(2);
                is = blob.getBinaryStream();
                byte[] b = new byte[1024];
                int len = is.read(b);
                while (len != -1) {
                    newStr = newStr + new String(b, "UTF-8");
                    len = is.read(b);
                }
                is.close();
            }
            newStr=newStr.trim();//必须加去除空格操作,否则无法解析。
            System.out.println(newStr);
            String result="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Ciqresult><CusCiqNo>bbb</CusCiqNo><EntDealNo></EntDealNo><ClBlNo></ClBlNo><OperTime>aaa</OperTime><RspCodes>ccc</RspCodes><RspInfo>XXX</RspInfo></Ciqresult>";
            System.out.println(result);
            try {
                Document doc = (Document) DocumentHelper.parseText(newStr);
                Element books = doc.getRootElement();
                System.out.println("根节点" + books.getName());
                String id = books.elementText("CusCiqNo");// 指定获取那个元素
                System.out.println(id);
                String ProofBillBarCode = books.elementText("OperTime");// 指定获取那个元素
                System.out.println(ProofBillBarCode);
                List e_proofInfo = books.elements("RspCodes");// 指定获取那个元素
                for (int i = 0; i < e_proofInfo.size(); i++) {//获取深层结构
                    Element n_proofInfo = (Element) e_proofInfo.get(i);
                    String eviId = n_proofInfo.attributeValue("RspInfo");
                    String proofInfo = n_proofInfo.getText();
                    System.out.println(eviId);
                    System.out.println(proofInfo);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                rs.close();
                sta.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    /**
     * 获得Connection
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

}

猜你喜欢

转载自blog.csdn.net/xianrenyingzi/article/details/80942235