简体   繁体   English

StAX 解析器在运行时确定

[英]StAX parser determination at runtime

I have the following code:我有以下代码:

XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(inStream);
this.encoding = xmlStreamReader.getEncoding();

... ...

This code runs fine in both JBoss and Websphere, however in a particular JBoss throws the following exception:这段代码在 JBoss 和 Websphere 中都运行良好,但是在特定的 JBoss 中会引发以下异常:

java.lang.ClassCastException: com.ctc.wstx.stax.WstxInputFactory cannot be cast to javax.xml.stream.XMLInputFactory
    at javax.xml.stream.XMLInputFactory.newInstance(XMLInputFactory.java:136)
    at es.gema.core.shared.dim.data.XFacturaE.detectVersion(XFacturaE.java:115)
    at es.gema.core.shared.dim.data.XFacturaE.<init>(XFacturaE.java:67)
    at es.gema.core.shared.dim.bc.InvoiceLoader.readXMLInvoice(InvoiceLoader.java:544)
    at es.gema.core.shared.dim.bc.InvoiceLoader.loadInvoiceFACE(InvoiceLoader.java:137)
    at es.gema.core.expenses.fac.bc.InvoiceServicesBC.execute(InvoiceServicesBC.java:127)
    at es.gema.core.expenses.fac.bc.InvoiceServicesBC.execute(InvoiceServicesBC.java:92)

Checking WstxInputFactory I see that it extends XMLInputFactory2 instead of XMLInputFactory.检查 WstxInputFactory 我看到它扩展了 XMLInputFactory2 而不是 XMLInputFactory。

What's the recommended approach in this case?在这种情况下推荐的方法是什么? Create an instance of WstxInputFactory without using the factory, or configure the Java container to return a parser that extends XMLInputFactory ?在不使用工厂的情况下创建 WstxInputFactory 的实例,或者配置 Java 容器以返回扩展 XMLInputFactory 的解析器?

public abstract class XMLInputFactory2
extends javax.xml.stream.XMLInputFactory

So com.ctc.wstx.stax.WstxInputFactory does extends javax.xml.stream.XMLInputFactory and must be therefore castable to it.所以com.ctc.wstx.stax.WstxInputFactory确实扩展了javax.xml.stream.XMLInputFactory ,因此必须可以com.ctc.wstx.stax.WstxInputFactory到它。

But since you're getting this exception, you must be running into a classloader issue.但是,由于您收到此异常,因此您一定遇到了类加载器问题。 Make sure that javax.xml.stream.XMLInputFactory is loaded by the same classloader.确保javax.xml.stream.XMLInputFactory由同一个类加载器加载。 Probably JBoss/JDK delivers one and your application also has a StAX in the classpath.可能 JBoss/JDK 提供了一个并且您的应用程序在类路径中也有一个 StAX。 But it's hard to tell who's guilty exactly.但很难说到底是谁有罪。

Problem: I was getting the below error.问题:我收到以下错误。 My server was "jboss-eap-5.1.1", JDK 1.6.我的服务器是“jboss-eap-5.1.1”,JDK 1.6。

java.lang.ClassCastException: com.ctc.wstx.stax.WstxOutputFactory cannot be cast to javax.xml.stream.XMLOutputFactory java.lang.ClassCastException:com.ctc.wstx.stax.WstxOutputFactory 无法转换为 javax.xml.stream.XMLOutputFactory

Solution: I've removed the "stax" library, precisely:解决方案:我已经删除了“stax”库,确切地说:

<exclusions>
    <exclusion>
        <groupId>javax.xml.stream</groupId>
        <artifactId>stax-api</artifactId>
    </exclusion>
</exclusions>

The solution with the stax-api exclusion works fine in the use of the Java Apache POI API for Microsoft Documents.带有 stax-api 排除项的解决方案在使用 Java Apache POI API for Microsoft Documents 时运行良好。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
    <exclusions>
        <exclusion>
            <groupId>stax</groupId>
            <artifactId>stax-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM