XML Paring  Error - Premature end of file.

Posted on 2016-09-27
Medium Priority
Last Modified: 2016-10-04
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
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.
Question by:MehtaJasmin
  • 3
  • 2
LVL 27

Accepted Solution

BigRat earned 2000 total points
ID: 41819505
It looks as if the InputStream was not set up properly. You'll have to post the code.
LVL 85

Expert Comment

by:David Johnson, CD, MVP
ID: 41820026
upload a sanitized copy of the file you are trying to read. you may only have

Author Comment

ID: 41820094
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);

                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  		        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  		        this.xmlDoc = docBuilder.parse(xmlSource);

Open in new window

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

LVL 27

Expert Comment

ID: 41820185
I have never understood the weird way of getting an XML "Document" for SAX parsing. But still I'm glad it's working.
LVL 27

Expert Comment

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

Author Closing Comment

ID: 41827916
Thanks for giving tip on InputStream

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question