org.xml.sax.SAXParseException: Premature end of file for *VALID* XML

前端 未结 7 763
梦如初夏
梦如初夏 2020-12-15 02:45

I am getting very strange \"Premature end of file.\" exception for last few days on one of our servers. The same configuration XML works fine on another server. We

相关标签:
7条回答
  • 2020-12-15 03:06

    This exception only happens if you are parsing an empty String/empty byte array.

    below is a snippet on how to reproduce it:

    String xml = ""; // <-- deliberately an empty string.
    ByteArrayInputStream xmlStream = new java.io.ByteArrayInputStream(xml.getBytes());
    Unmarshaller u = JAXBContext.newInstance(...)
    u.setSchema(...);
    u.unmarshal( xmlStream ); // <-- here it will fail
    
    0 讨论(0)
  • 2020-12-15 03:11

    It is a problem with Java InputStream. When the stream is read once the file offset position counter is moved to the end of file. On the subsequent read by using the same stream you'll get this error. So you have to close and reopen the stream again or call inputStream.reset() to reset the offset counter to its initial position.

    0 讨论(0)
  • 2020-12-15 03:19

    If input stream is not closed properly then this exception may happen. make sure : If inputstream used is not used "Before" in some way then where you are intended to read. i.e if read 2nd time from same input stream in single operation then 2nd call will get this exception. Also make sure to close input stream in finally block or something like that.

    0 讨论(0)
  • 2020-12-15 03:20

    Please make sure that you are not consuming your inputstream anywhere before parsing. Sample code is following: the respose below is httpresponse(i.e. response) and main content is contain inside StringEntity (i.e. getEntity())in form of inputStream(i.e. getContent()).

    InputStream rescontent = response.getEntity().getContent();
    tsResponse=(TsResponse) transformer.convertFromXMLToObject(rescontent );
    
    0 讨论(0)
  • 2020-12-15 03:23

    In our case it was an empty AndroidManifest.xml.

    While upgrading Eclispe we ran into the usual trouble, and AndroidManifest.xml must have been checked into SVN by the build script after being clobbered.

    Found it by compiling from inside Eclipse, instead of from the command line.

    0 讨论(0)
  • 2020-12-15 03:24

    This is resolved. The problem was elsewhere. Another code in cron job was truncating XML to 0 length file. I have taken care of that.

    0 讨论(0)
提交回复
热议问题