Forum Stats

  • 3,837,116 Users
  • 2,262,228 Discussions
  • 7,900,208 Comments

Discussions

Writing BLOB into XML file

2703616
2703616 Member Posts: 3
edited Jan 3, 2017 2:23AM in Java Technology & XML

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-

    [email protected]

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

    }

Answers

  • 2703616
    2703616 Member Posts: 3
    edited Jan 3, 2017 2:23AM

    For some reason, the following code snippet worked-

    <span class="typ">BufferedReader</span><span class="pln"> br </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">BufferedReader</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">InputStreamReader</span><span class="pun">(</span><span class="pln">is</span><span class="pun">));</span><span class="pln"><br/></span><span class="typ">StringBuilder</span><span class="pln"> sb </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">StringBuilder</span><span class="pun">();</span><span class="pln"><br/></span><span class="typ">String</span><span class="pln"> line </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><span class="pln"><br/></span><span class="kwd">while</span><span class="pln"> </span><span class="pun">((</span><span class="pln">line </span><span class="pun">=</span><span class="pln"> br</span><span class="pun">.</span><span class="pln">readLine</span><span class="pun">())</span><span class="pln"> </span><span class="pun">!=</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br/>    sb</span><span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="pln">line</span><span class="pun">);</span><span class="pln"><br/></span><span class="pun">}</span><span class="pln"><br/></span><span class="typ">System</span><span class="pun">.</span><span class="pln">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="pln">sb</span><span class="pun">.</span><span class="pln">toString</span><span class="pun">());</span>

    But I'm still very curious to understand why my previous approach didn't work when I was reading the data from the database, but worked just fine when I directly read it from a physical location like C:\ drive.

This discussion has been closed.