Link to home
Start Free TrialLog in
Avatar of MehtaJasmin
MehtaJasminFlag for United States of America

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.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of
file.
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.
java:257)
ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of David Johnson, CD
upload a sanitized copy of the file you are trying to read. you may only have
<tablename>data</tablename></xml>
Avatar of MehtaJasmin

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/InputSource 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

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

Open in new window

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.
Thanks for giving tip on InputStream