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());
}
For some reason, the following code snippet worked-