Man*_*ish 45 java xml xml-parsing
我变得非常奇怪"文件过早结束." 我们的一台服务器上的最近几天例外.在相同的配置XML工作正常另一台服务器上.我们在这两台服务器上都使用Tomcat 5.0.28.此代码已经工作了很长时间(7年以上),只有在最近服务器崩溃后,我们才在其中一台服务器上遇到此问题.XML和Java解析代码没有变化.:(
我能看到的唯一区别是Java版本 -
问题服务器 java版"1.6.0_16"Java(TM)SE运行时环境(版本1.6.0_16-b01)Java HotSpot(TM)64位服务器VM(版本14.2-b01,混合模式)
工作服务器 java版"1.6.0_07"Java(TM)SE运行时环境(版本1.6.0_07-b06)Java HotSpot(TM)64位服务器VM(版本10.0-b23,混合模式)
以下是已经工作了几年的Java代码 -
private void readSource(final InputSource in ) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(in);
Element elt = doc.getDocumentElement();
this.readElement( elt );
} catch ( Exception ex ) {
ex.printStackTrace();
throw new ConfigurationException( "Unable to parse configuration information", ex );
}
}
Run Code Online (Sandbox Code Playgroud)
这是例外.
[Fatal Error] :-1:-1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at com.circus.core.Configuration.readSource(Configuration.java:706)
Run Code Online (Sandbox Code Playgroud)
我已经尝试过验证XML并且没有发现错误.知道在哪里可以寻找可能的问题吗?
任何指针都将受到高度赞赏!
TIA, - Manish
小智 63
这是Java InputStream的一个问题.一旦文件偏移位置计数器移动到文件末尾,就会读取流.在随后使用相同的流读取时,您将收到此错误.因此,您必须再次关闭并重新打开流,或者调用inputStream.reset()将偏移计数器重置为其初始位置.
Man*_*ish 22
这已经解决了.问题出在其他地方.cron作业中的另一个代码是将XML截断为0长度文件.我照顾好了.
Zo7*_*o72 17
只有在解析空字符串/空字节数组时才会发生此异常.
下面是如何重现它的片段:
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
Run Code Online (Sandbox Code Playgroud)
小智 5
inputstream解析之前,请确保您不消耗任何空间。示例代码如下:下面的代码是httpresponse(即响应),主要内容包含在内部StringEntity (i.e. getEntity())in form of inputStream(i.e. getContent())。
InputStream rescontent = response.getEntity().getContent();
tsResponse=(TsResponse) transformer.convertFromXMLToObject(rescontent );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
175823 次 |
| 最近记录: |