Premature end of file 错误解决

http://blog.sina.com.cn/s/blog_409994cb0100oj2t.html


 最近在做一个打印的工作,需要对输入流的xml信息进行解析。为了能够满足多元化的需求,这个输入流需要解析多次,使用同一个方法进行解析,在第二次的时候就会抛出一个异常,解析后的document对象也为null。

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

  百思不得其解,昨日想了一个应对方法,将输入流先读取到一个byte[]对象中,在第二次解析的时候,使用这个byte[]来解决。结果问题更加郁闷,在第一次调用解析的时候,就报错了。欲哭无泪啊……

  任务就是任务,不管你有多少困难都要迎头赶上,继续埋头苦思问题的所在。还好如今互联网发达了,可以让我找到问题的源头。参考文献如下:

 

  使用dom4j解析来自InputStream中的XML内容,发生异常:Premature end of file。是因为这个InputStream已经读过了,在读入dom4j时,不是从开头读的,因此报错。

  详细解释如下:来自http://forums.sun.com/thread.jspa?threadID=641465非常遗憾,sun被oracle收购了,所以这个地址不存在了……

  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.

 

  还别说,我之前想到的那个先读入byte[]的想法还是比较好的,只要后续的操作使用new ByteArrayInputStream(tbyte),就没有再出现解析的问题。


猜你喜欢

转载自blog.csdn.net/Grace_1203/article/details/80980121
今日推荐