简体   繁体   English

org.xml.sax.SAXParseException:*VALID* XML 的文件过早结束

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

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.相同的配置 XML 在另一台服务器上运行良好。 We are using Tomcat 5.0.28 on both these servers.我们在这两台服务器上都使用 Tomcat 5.0.28。 This code has been working for ages (7+ years), only after recent server crash, we faced this problem on one of the servers.这段代码已经工作了很长时间(7 年以上),只有在最近的服务器崩溃之后,我们才在其中一台服务器上遇到了这个问题。 There is no change in XML as well as Java parsing code. XML 和 Java 解析代码没有变化。 :( :(

The only difference I can see is in Java versions -我能看到的唯一区别是 Java 版本 -

Problem Server java version "1.6.0_16" Java(TM) SE Runtime Environment (build 1.6.0_16-b01) Java HotSpot(TM) 64-Bit Server VM (build 14.2-b01, mixed mode)问题服务器java 版本“1.6.0_16”Java(TM) SE 运行时环境(构建 1.6.0_16-b01)Java HotSpot(TM) 64 位服务器 VM(构建 14.2-b01,混合模式)

Working Server java version "1.6.0_07" Java(TM) SE Runtime Environment (build 1.6.0_07-b06) Java HotSpot(TM) 64-Bit Server VM (build 10.0-b23, mixed mode)工作服务器java 版本“1.6.0_07” Java(TM) SE 运行时环境(构建 1.6.0_07-b06)Java HotSpot(TM) 64 位服务器 VM(构建 10.0-b23,混合模式)

Here is the Java code that has been working for several years -这是已经工作了几年的 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 );
    }
}

And here is the exception.这是个例外。

[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)

I have already tried validating XML and found no errors there.我已经尝试验证 XML 并没有发现任何错误。 Any idea where else can I look for possible problem?知道我还能在哪里寻找可能的问题吗?

Any pointers would be highly appreciated!任何指针将不胜感激!

TIA, - Manish TIA, - 马尼什

It is a problem with Java InputStream. 这是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. 因此,您必须再次关闭并重新打开流,或调用inputStream.reset()将偏移计数器重置为其初始位置。

This is resolved. 这已经解决了。 The problem was elsewhere. 问题出在其他地方。 Another code in cron job was truncating XML to 0 length file. cron作业中的另一个代码是将XML截断为0长度文件。 I have taken care of that. 我照顾好了。

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

Please make sure that you are not consuming your inputstream anywhere before parsing. 在解析之前,请确保您没有在任何地方消耗输入inputstream Sample code is following: the respose below is httpresponse (ie response) and main content is contain inside StringEntity (ie getEntity())in form of inputStream(ie getContent()) . 示例代码如下:下面的httpresponsehttpresponse (即响应),主要内容包含在StringEntity (ie getEntity())in form of inputStream(ie getContent())

InputStream rescontent = response.getEntity().getContent();
tsResponse=(TsResponse) transformer.convertFromXMLToObject(rescontent );

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. 确保:如果使用的输入流未以某种方式使用“之前”,那么您打算阅读。 ie 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. 还要确保在finally块或类似的东西中关闭输入流。

Are you sure that the XML file is in the correct character encoding? 您确定XML文件的字符编码正确吗? FileReader always uses the platform default encoding, so if the "working" server had a default encoding of (say) ISO-8859-1 and the "problem" server uses UTF-8 you would see this error if the XML contains any non-ASCII characters. FileReader始终使用平台默认编码,因此如果“工作”服务器的默认编码为(例如)ISO-8859-1,而“问题”服务器使用UTF-8,如果XML包含任何非错误,您将看到此错误ASCII字符。

Does it work if you create the InputSource from a FileInputStream instead of a FileReader? 如果从FileInputStream而不是FileReader创建InputSource,它是否有效?

In our case it was an empty AndroidManifest.xml . 在我们的例子中,它是一个空的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. 在升级Eclispe时,我们遇到了常见的麻烦 ,AndroidManifest.xml必须在被破坏后由构建脚本检入SVN。

Found it by compiling from inside Eclipse, instead of from the command line. 通过从Eclipse内部编译而不是从命令行编译它。

NEW Occurence of this same error on DECEMBER 6 2021!!! NEW 2021 年 12 月 6 日发生同样的错误!!!

Sample traces:样本跟踪:

XmlBeanDefinitionStoreException: Line -1 in XML document from ServletContext resource [<here a reference to spring context .xml file>] is invalid; nested exception is org.xml.sax.SAXParseException; Premature end of file.
Caused by: org.xml.sax.SAXParseException; Premature end of file.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:201)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:175)
at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:398)
at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:282)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:204)
at org.apache.xerces.impl.xs.opti.SchemaParsingConfig.parse(SchemaParsingConfig.java:576)
at org.apache.xerces.impl.xs.opti.SchemaParsingConfig.parse(SchemaParsingConfig.java:679)
at org.apache.xerces.impl.xs.opti.SchemaDOMParser.parse(SchemaDOMParser.java:527)
at org.apache.xerces.impl.xs.traversers.XSDHandler.getSchemaDocument(XSDHandler.java:2148)
at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:557)

The HINT:提示:

  1. Apache CAMEL team decided to reorganize CAMEL schema files as usually referenced in XML Spring contexts for instance. Apache CAMEL 团队决定重新组织 CAMEL 模式文件,例如通常在 XML Spring 上下文中引用的文件。 Instead of just removing (which would have entailed a neat exception, easy to spot) the http://camel.apache.org/schema/spring/camel-spring-2.16.4.xsd usual schema location, they implemented a HTTP 301 redirect response to HTTPS.而不是仅仅删除(这会带来一个简洁的异常,很容易发现) http://camel.apache.org/schema/spring/camel-spring-2.16.4.xsd通常的模式位置,他们实现了一个 HTTP 301将响应重定向到 HTTPS。

  2. The Apache XERCES library does not throw an error in case of HTTP 301, but assumes it has received an empty file!! Apache XERCES 库不会在 HTTP 301 的情况下抛出错误,而是假设它收到了一个空文件!! That entails weird and erroneous exceptions / putting you on the wrong track as these report the failure linked to the top XML file and not the actually offending schema descriptor这需要奇怪和错误的异常/让你走上错误的轨道,因为这些报告链接到顶级 XML 文件的失败,而不是实际有问题的模式描述符

  3. the complete revalidation of all schema descriptors and their XSD dependencies at WAR reload/deploy time (from the data/content repo cache) in our app server platform was totally unexpected... and is totally useless!在 WAR 重新加载/部署时(来自数据/内容存储库缓存)在我们的应用程序服务器平台中完全重新验证所有模式描述符及其 XSD 依赖项是完全出乎意料的......而且完全没用! worse: creating a runtime public network dependency for just reloading already validated components and descriptors (at build time)更糟糕的是:创建运行时公共网络依赖项,以便重新加载已验证的组件和描述符(在构建时)

1+2+3 above, and BANG! 1+2+3 以上,砰! major service disruption: the production server was unable to reload any component with a dependency on CAMEL主要服务中断:生产服务器无法重新加载依赖于 CAMEL 的任何组件

Fixings:固定装置:

Two candidates:两位候选人:

a) in xsi:schemaLocation attributes in XML simply add 's' to the http://camel.apache.org/schema/etc becoming https://.... but accept to live with a public network dependency on every component reload a) 在 XML 中的 xsi:schemaLocation 属性中,只需将“s”添加到http://camel.apache.org/schema/etc变成 https://.... 但接受对每个组件的公共网络依赖重新加载

b) replace all http://... shema locations by classpath:. b) 用类路径替换所有 http://... shema 位置:。 You will have downloaded all XSD and sub-dependent schemas, and deploy them along with the WAR ensuring a visibility to the classloader.您将下载所有 XSD 和子依赖模式,并将它们与 WAR 一起部署,以确保对类加载器的可见性。 Eg put the files in java/main/resources/somename.xsd and supply classpath:somename.xsd as schema location path例如,将文件放在 java/main/resources/somename.xsd 并提供 classpath:somename.xsd 作为模式位置路径

@berhauz, Any additional comment? @berhauz,还有其他评论吗? I have the same issue and have not have success doing the 1st option better thing I have achieved is: Status: GracePeriod Blueprint 12/6/21 9:23 PM Missing dependencies: (&(objectClass=org.apache.aries.blueprint.NamespaceHandler)(osgi.service.blueprint.namespace=https://www.w3.org/2001/XMLSchema-instance))我有同样的问题并且没有成功做我已经实现的第一个选项:状态:GracePeriod Blueprint 12/6/21 9:23 PM 缺少依赖项:(&(objectClass=org.apache.aries.blueprint. NamespaceHandler)(osgi.service.blueprint.namespace=https://www.w3.org/2001/XMLSchema-instance))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.xml.sax.SAXParseException:过早的结束文件 - org.xml.sax.SAXParseException: Premature end file 轴:faultString:org.xml.sax.SAXParseException:文件过早结束 - Axis: faultString: org.xml.sax.SAXParseException: Premature end of file org.xml.sax.SAXParseException:带有jaxbUnmarshaller的文件过早结束 - org.xml.sax.SAXParseException: Premature end of file with jaxbUnmarshaller 引起原因:org.xml.sax.SAXParseException:文件过早结束 - Caused by: org.xml.sax.SAXParseException: Premature end of file org.xml.sax.SAXParseException:文件过早结束 - org.xml.sax.SAXParseException: Premature end of file org.xml.sax.SAXParseException:文件过早结束 - org.xml.sax.SAXParseException: Premature end of file 文件过早结束。 org.xml.sax.SAXParseException; 文件过早结束。[致命错误]:-1:-1: - Premature end of file. org.xml.sax.SAXParseException; Premature end of file.[Fatal Error] :-1:-1: org.xml.sax.SAXParseException; 尝试从请求中获取 xml 输入时文件过早结束 - org.xml.sax.SAXParseException; Premature end of file when trying to get an xml input from request 解析RSS时出错-&gt; org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1; 文件过早结束 - Error Parsing RSS -> org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file Java JaxB Unmarshaller提供org.xml.sax.SAXParseException文件的过早结尾 - Java JaxB Unmarshaller giving org.xml.sax.SAXParseException Premature end of file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM