Getting Premature end of file Exception

34,105

Solution 1

If you get this error at the first position of the file (which the 1:1 indicates) it means that the file is empty.

Maybe you start reading the file before the source has closed it?

In case you use an input stream (which is not the case here), this can happen when you re-use a stream that you have already used to reach the end of the file. You need to create a new stream from the input file in order to reset it from the start of the file.

Solution 2

The message indicates that you have a badly formed XML file. Usually when I've gotten this message I had an opening tag with no matching end tag. I think you'll also get this on an empty file.

Share:
34,105
ramesh
Author by

ramesh

Updated on November 23, 2020

Comments

  • ramesh
    ramesh over 3 years

    I'm trying to parse the existing xhtml file to add the additional body content into that file. I am using the following code:

    First I am reading the body from the Jsoup and i am trying to put it in the XhtmlFile

    Document doc = Jsoup.parse(readFile, "UTF-8");
                Elements content = doc.getElementsByTag("body");
    
                try {
                     Document document=null;
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    // Create the builder and parse the file
                    document = (Document)factory.newDocumentBuilder().parse(finalFile);
                    //document.getElementsByTagName("body")append(content.toString());              
                    //document=parserXML(finalFile);                
                    document.getElementsByTag("body").append(content.toString());
    
                } catch (SAXException e) {
                    System.out.println("SAXException>>>>>>");
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    System.out.println("in  parser configuration Exception block>>>>>>");
                    e.printStackTrace();
                }
    

    But i am getting the following exception:

    [Fatal Error] ResultParsedFile.html:1:1: Premature end of file.
    org.xml.sax.SAXParseException: Premature end of file.
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
        at com.converter.typeconverter.EmailTypeConverter.readHTML(EmailTypeConverter.java:101)
        at com.converter.typeconverter.EmailTypeConverter.callTika(EmailTypeConverter.java:64)
        at com.converter.master.ApplicationMain.main(ApplicationMain.java:64)
    

    Plese help me in solving this issue ...

    Thanks in advance...

    • Jon Skeet
      Jon Skeet over 12 years
      Sounds like finalFile probably isn't a valid XML file...
    • Artem
      Artem over 12 years
      If it's XHTML, you should be able to parse it with a plain XML parser. Jsoup is not especially aimed at broken !X!HTML.
    • ramesh
      ramesh over 12 years
      @Jon means i may not use DOM or SAX parsers to parse, but from jsoup i am getting the body content of the html file which i need, i just want inject that message into the body of the xhtml(finalfile).thanks for your time. Any pointer in this regard..
    • Jon Skeet
      Jon Skeet over 12 years
      @ramesh: But you're trying to parse finalFile before you do anything with content. If that's not already a valid XML file, you'll have a problem...
    • ramesh
      ramesh over 12 years
      @Jon: So i want to first append the content to the finalFile before doing the parsing.
    • Jon Skeet
      Jon Skeet over 12 years
      @ramesh: Are you expecting finalFile to be empty before you start then? It's really not at all clear what's going on...
    • Gaurav
      Gaurav over 12 years
      Is the EmailConverter some third party library ? Is there any '@' character somewhere at in-appropriate place in XML ?
  • ramesh
    ramesh over 12 years
    I m using xhtml file, in which i want add some body part. but the skeleton xhtml page with proper opening and closing tags are available.