Solved

XML Paring  Error - Premature end of file.

Posted on 2016-09-27
7
448 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 81

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

717 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