Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Writing BLOB into XML file

2703616Jan 3 2017 — edited Jan 3 2017

I am using `Oracle 11g` database and `jdk1.6.0_45`

I am attempting to fetch an `xml` file in the form of a `blob` from the database and using `DOM parser`. I'm trying to parse the `blob` object and write the output content in the console. Here is my code snippet-

    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);

And here is the Exception with stacktrace-

    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"?>

I've checked the validity of the XML file. After the first line `<?xml version="1.0" encoding="UTF-8" standalone="no"?>` there is a valid `<tag>`

However, I'm successfully able to read an `xml` file from a physical location like `C:\` drive and write it to the output xml. This is how I'm doing it (keeping in mind that fetching the `InputStream` data and remaining steps after that are common) -

    java.io.File file = new File("C:\\myLocalXMLFile.xml");

  

    InputStream is = new FileInputStream(file);

    outputFromInputStream(is);

Here is the common method used-

    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());

    }

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 31 2017
Added on Jan 3 2017
1 comment
4,568 views