Avatar of MehtaJasmin
MehtaJasmin
Flag 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)
JavaXML

Avatar of undefined
Last Comment
MehtaJasmin

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
BigRat

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
David Johnson, CD

upload a sanitized copy of the file you are trying to read. you may only have
<tablename>data</tablename></xml>
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

BigRat

I have never understood the weird way of getting an XML "Document" for SAX parsing. But still I'm glad it's working.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
BigRat

Sorry, but I did suggest that you look at InputStream which you did and that fixed the problem.
MehtaJasmin

ASKER
Thanks for giving tip on InputStream