将BLOB写入XML文件

时间:2017-01-03 05:28:51

标签: oracle11g java-6 saxparseexception

我正在使用Oracle 11g数据库和jdk1.6.0_45 我试图从数据库中使用xmlblob的形式从DOM parser获取blob文件。我试图解析String outputFile = "C:\\DEVELOPMENT_PROJECTS\\result1.xml"; String myDocId = "12345"; java.sql.PreparedStatement pstmt = connection.prepareStatement("SELECT * FROM my_document WHERE my_doc_id = '" + myDocID + "'"); // connection is of type java.sql.Connection java.sql.ResultSet rs = pstmt.executeQuery(); // always returns only one row. rs.next(); java.sql.Blob blobObj = rs.getBlob("DOC_BLOB"); java.io.InputStream is = blobObj.getBinaryStream(); java.io.FileOutputStream fos = new FileOutputStream(outputFile); while (((byte) is.read()) != -1) { fos.write((byte) is.read()); } // tried writing in output xml file too. this writes junk data into the output file outputFromInputStream(is); 对象并在控制台中写入输出内容。这是我的代码片段 -

oracle.jdbc.driver.T4CConnection@19a32e0
[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
    at com.dummy.project.lang.DataTest.newDocumentFromInputStream(DataTest.java:197)
    at com.dummy.project.lang.DataTest.main(DataTest.java:75)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>

以下是堆栈跟踪的异常 -

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

我已检查过XML文件的有效性。在第一行<tag>之后,有一个有效的xml

但是,我已成功从C:\驱动器等物理位置读取InputStream文件,并将其写入输出xml。这就是我这样做的方式(请记住,获取java.io.File file = new File("C:\\myLocalXMLFile.xml"); InputStream is = new FileInputStream(file); outputFromInputStream(is); 数据并在此之后剩下的步骤很常见) -

private void outputFromInputStream(InputStream is) {
javax.xml.parsers.DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();

org.w3c.dom.Document doc = builder.parse(in); // Exception is thrown here

java.io.StringWriter sw = new StringWriter();

javax.xml.transform.dom.DOMSource domSource = new DOMSource(xmldom);

javax.xml.transform.stream.StreamResult streamResult = new StreamResult(sw);

javax.xml.transform.TransformerFactory tf = TransformerFactory.newInstance();

javax.xml.transform.Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
transformer.setOutputProperty("http://xml.apache.org/xslt}indent-amount", "10");
transformer.transform(domSource, streamResult);
System.out.println(sw.toString());
}

这是常用的方法 -

public class CommonUtilities {

    private static SharedPreferences.Editor editor;
    private static SharedPreferences sharedPreferences;
    private static Context mContext;

/**
     * Create SharedPreference and SharedPreferecne Editor for Context
     *
     * @param context
     */
    private static void createSharedPreferenceEditor(Context context) {
        try {
            if (context != null) {
                mContext = context;
            } else {
                mContext = ApplicationStore.getContext();
            }
            sharedPreferences = context.getSharedPreferences(IAdoddleConstants.ADODDLE_PREF, Context.MODE_PRIVATE);
            editor = sharedPreferences.edit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

/**
 * Put String in SharedPreference Editor
 *
 * @param context
 * @param key
 * @param value
 */
public static void putPrefString(Context context, String key, String value) {
    try {
        createSharedPreferenceEditor(context);
        editor.putString(key, value);
        editor.commit();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

}

1 个答案:

答案 0 :(得分:0)

出于某种原因,以下代码段有效 -

BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
    sb.append(line);
}
System.out.println(sb.toString());