Solved

XML Paring  Error - Premature end of file.

Posted on 2016-09-27
7
78 Views
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
file.
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.
java:257)
0
Comment
Question by:MehtaJasmin
  • 3
  • 2
7 Comments
 
LVL 27

Accepted Solution

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

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
<tablename>data</tablename></xml>
0
 

Author Comment

by:MehtaJasmin
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);
                xmlSource.setEncoding("UTF-8");                

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

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 27

Expert Comment

by:BigRat
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.
0
 
LVL 27

Expert Comment

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

Author Closing Comment

by:MehtaJasmin
ID: 41827916
Thanks for giving tip on InputStream
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# code 19 69
simple java question 3 44
Java Loop 6 49
Groovy problem when using SOAPUI : DispatchException occurred 7 27
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now