MehtaJasmin
asked on
XML Paring Error - Premature end of file.
I am decrypting XML file and in memory, I am parsing it for searching particular XML element. If I store the file on disk after decypting and then read that file for parsing, program works fine. But when I decrypt the file and pass it to DOM Parser as InputStream then I am getting this exception. What possibly I am doing wrong:
[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseExcept ion; lineNumber: 1; columnNumber: 1; Premature end of
file.
at com.sun.org.apache.xerces. internal.p arsers.DOM Parser.par se(DOMPars er.
java:257)
[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseExcept
file.
at com.sun.org.apache.xerces.
java:257)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I fixed the issue indirectly by doing the needful for the InputStream object within the calling method. Rather than caller method.
Below is now the code inside method decrypt(). And in main() method I am using the Document object. If I get InputStream/Reader/InputSo urce by invoking this decrypt() method in main(), it does not keep the stream object and gives me the same 'premature end of file' exception. But if I also build the Document object in the decrypt() method and than invoke/use that object in main() method, I don't get the exception.
xmlDoc is Document object
Below is now the code inside method decrypt(). And in main() method I am using the Document object. If I get InputStream/Reader/InputSo
xmlDoc is Document object
InputStream unc = ld.getInputStream();
Reader reader = new InputStreamReader(unc,"UTF-8");
InputSource xmlSource = new InputSource(reader);
xmlSource.setEncoding("UTF-8");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
this.xmlDoc = docBuilder.parse(xmlSource);
I have never understood the weird way of getting an XML "Document" for SAX parsing. But still I'm glad it's working.
Sorry, but I did suggest that you look at InputStream which you did and that fixed the problem.
ASKER
Thanks for giving tip on InputStream
<tablename>data</tablename