1 2 Previous Next 18 Replies Latest reply on Aug 13, 2010 3:48 PM by 843834

    Premature End Of File - SaxParseException

    843834
      Hello everyone,

      everytime i try to access an XML File i get the following error output:

      [Fatal Error] :-1:-1: Premature end of file.
      org.xml.sax.SAXParseException: Premature end of file.

      The XML file looks like this:

      <?xml version="1.0" encoding="UTF-8"?><Rezept><Name>test<Kochbuch>test</Kochbuch><Seite>23</Seite><Zutaten><Zutat Menge="2">eier</Zutat></Zutaten><Anleitung>Geben Sie hier das Rezept ein</Anleitung></Name></Rezept>

      Well, it's German but maybe you could tell me whats wrong with that. The code that tries to access the XML file is the following:
      try {
                      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                      DocumentBuilder builder = factory.newDocumentBuilder();
                      doc = builder.parse(file);
                  } catch (ParserConfigurationException e) {
                      new ExceptionFrame(e.toString());
                  } catch(SAXException ex) {
                      ex.printStackTrace();
                  } catch(IOException ex) {
                      ex.printStackTrace();
                  }
      This is only called - and it only happens - when the file exists.
      So, like usual, i am very happy for any advice that lead me to the right direction and to make me see the light :-)

      Chris
        • 1. Re: Premature End Of File - SaxParseException
          3004
          Mmmh the file looks OK and the code looks OK, so it is probably a problem that does not directly meets the eye.

          I remember having a similar issue while parsing XML files created with some MS XML tool of some sort: the thingy was putting to bytes in the beginning of the XML file, before the XML prolog.

          These 2 bytes were not needed at all in a UTF-8 encoded file so I had to knock them off.

          Maybe it is worth trying to HEX view your XML file and see if there are any suckers before the prolog?
          • 2. Re: Premature End Of File - SaxParseException
            843834
            Thanks for your answer. But could it be that it has something to do with the OS. As far as i remember the code worked fine on Windows, but now i started using Mac OS X, so maybe there's the problem. Just an idea...
            • 3. Re: Premature End Of File - SaxParseException
              3004
              Yep, it could be. Have you checked your file in hexa?
              • 4. Re: Premature End Of File - SaxParseException
                843834
                I'm having a similar problem. I'm sending an image as a blob in xml, and I think it's either too big or there are some strange characters in it. It's more than a meg, so I haven't looked through all of it yet. Any Ideas? I'll let you know if I figure anything out.
                • 5. Re: Premature End Of File - SaxParseException
                  843834
                  Hi
                  Yes, you do get a CTRL + M character at the end of the XML docuement when you port the data from Windows to UNIX/Linux. In order to avoid this you could either strip off these by getting the ASCII data only and then forming the new XML by reading byt by byte or ask the provider (if any) to make this compatible with your OS.
                  • 6. Re: Premature End Of File - SaxParseException
                    843834
                    Did anybody solved this problem or know the cause of it
                    • 7. Re: Premature End Of File - SaxParseException
                      843834
                      Did anybody solved this problem or know the cause of
                      it
                      I think I found a improper closed tag in your XML, though.
                      <Name></name>. I really think it was the problem not your OS.

                      <?xml version="1.0" encoding="UTF-8"?>
                      <Rezept>
                      <Name>test
                      <Kochbuch>test</Kochbuch>
                      <Seite>23</Seite>
                      <Zutaten>
                      <Zutat Menge="2">eier</Zutat>
                      </Zutaten>
                      <Anleitung>Geben Sie hier das Rezept ein</Anleitung>
                      </name>
                      </Rezept>

                      Good luck :D

                      Oh.. by the way, I'm also cross-work my XML on Windows and Sun UNIX. No problem, up to present.

                      Message was edited by:
                      TikkyChai
                      • 8. Re: Premature End Of File - SaxParseException
                        843834
                        hi all,

                        I also faced a similar problem while using Axis, but it got resolved the moment i have given some values to the mandatory elements in the output XSD.Just check your output carefully for any mandatory elements,if any of the mandatory element is null or empty,the Axis used to through Premature end of file...

                        Thanks
                        Vivek Shastri
                        • 9. Re: Premature End Of File - SaxParseException
                          843834
                          I was getting this same error "Premature End Of File" , but there was no problem with my XML file. I checked my xml file for special characters before the prolog by using a hex editor and no problems were detected.

                          However, there was some problem in my JSTL code -

                          I had
                          <c:import url="sample.xml" var="xml_doc" charEncoding="UTF-8"/>
                          <x:parse doc="${xml}" var="xml_doc"/>

                          when I changed this to
                          <c:import url="sample.xml" var="xml" charEncoding="UTF-8"/>
                          <x:parse doc="${xml}" var="xml_doc"/>

                          I stopped getting the error. and everything worked fine as before
                          • 10. Re: Premature End Of File - SaxParseException
                            843834
                            Hi,

                            I got the same error, and solved by soing this.

                            Problem: Input stream is opened and read bytes from it, passed the same to document builder to parse method. so it caused the exception saying premature end of file.

                            Solution: Pass fresh input stream which is opened and not read anything (bytes) before passing to parse method of DocumentBuilder object.

                            Regards,
                            Venkat S
                            • 11. Re: Premature End Of File - SaxParseException
                              843834
                              That is a common problem when the contents of the xml file were written by your program using a PrintWriter or something similar and you forgot to call flush(). If that is the case, then make sure that you invoke the flush() method after you are done writing to the xml file to ensure everything in the buffer gets into the file.
                              File temp = new File("XmFile");
                              BufferedWriter bw = null;
                              Document doc = null;
                              String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOMETAG>Content</SOMETAG>";
                              try
                              {
                                  PrintWriter w = new PrintWriter(temp);
                                  bw = new BufferedWriter(w);
                                  bw.write(xml);
                                  bw.flush(); //<---- Make sure you flush the buffer contents into the file.
                              
                                 //Now you should be ok to read the xml contents
                                 Document doc = null;
                                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                                 DocumentBuilder builder = factory.newDocumentBuilder();
                                 doc = builder.parse(temp);
                               }
                               catch (Exception e)
                                {
                                     e.printStackTrace();
                                }
                              .....
                              • 12. Re: Premature End Of File - SaxParseException
                                843834
                                Hello,

                                I'm trying to do the following and get the same error message:
                                String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOMETAG>Content</SOMETAG>";
                                InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());
                                And then I use this InputStream to create a JAXB Object:
                                     Object obj = null;
                                     try {
                                          JAXBContext context = JAXBContext.newInstance(ObjectFactory.class); //points to the right ObjectFactory class
                                          Unmarshaller unmarshaller = context.createUnmarshaller();
                                          unmarshaller.setSchema(getSchema()); //gets the Schema
                                          obj = unmarshaller.unmarshal(xmlStream);
                                     } catch (JAXBException e){
                                          LOG.error("Unable to convert the inputstream into an xml representation.",e);
                                     } catch (SAXException e) {
                                          LOG.error("Unable to get Schema.",e);
                                     }
                                This results in the error message:
                                Unable to convert the inputstream into an xml representation.
                                javax.xml.bind.UnmarshalException
                                 - with linked exception:
                                [org.xml.sax.SAXParseException: Premature end of file.]
                                     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
                                     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:476)
                                     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:198)
                                     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:167)
                                     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
                                     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
                                ...
                                So I import the schema locally, I use a ByteArrayInputStream that doesn't have a flush method, I printed out the String and the content of the ByteArrayInputStream. I can't find any invalid or special characters. The xml in the String is a valid xml which should be converted correctly.

                                Any ideas what the problem could be?
                                • 13. Re: Premature End Of File - SaxParseException
                                  DrClap
                                  So your unmarshaller is reading that document you posted there and a schema. You have convinced yourself (and me) that the document you posted is satisfactory. That leaves the schema...
                                  • 14. Re: Premature End Of File - SaxParseException
                                    843834
                                    Actually, it's a part of a flow.
                                    I do a WebService call and get an xml response.
                                    I write it to a file using a Marshaller.
                                    At a later point, I want to make a response to the customer and include this xml response.
                                    I want to open it with an InputStream and unmarshall it so I can add it to the result.

                                    The piece of code is just a TestCase where I paste the response from the WebService into a String and make an InputStream from it.

                                    So Marshalling works, but Unmarshalling results in this error...

                                    Most causes of this error message mention an invalid xml or file. Or a schema that is not used or incorrect.
                                    imho I don't see how this can be in my case because Marshalling succeeds and I don't use a file in my TestCase, I use a String that is converted correctly to an InputStream.

                                    If I make the content of the xml in the String invalid, I get the message expected content is: and then a list of elements that are expected by the schema.

                                    In my case I don't need to work with the response as an Object anymore so instead, I just convert the InputStream into a String and add this to the response, but I would like to know why I got the error message...
                                    1 2 Previous Next