StAX parser determination at runtime
Asked Answered
I

3

5

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:

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.

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 ?

Incision answered 26/11, 2014 at 11:35 Comment(0)
M
6
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.

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. Probably JBoss/JDK delivers one and your application also has a StAX in the classpath. But it's hard to tell who's guilty exactly.

Marmot answered 26/11, 2014 at 13:57 Comment(1)
Could you please expand your answer adding some details about what you mean by "Make sure that javax.xml.stream.XMLInputFactory is loaded by the same classloader" ?Girhiny
P
3

Problem: I was getting the below error. My server was "jboss-eap-5.1.1", JDK 1.6.

java.lang.ClassCastException: com.ctc.wstx.stax.WstxOutputFactory cannot be cast to javax.xml.stream.XMLOutputFactory

Solution: I've removed the "stax" library, precisely:

<exclusions>
    <exclusion>
        <groupId>javax.xml.stream</groupId>
        <artifactId>stax-api</artifactId>
    </exclusion>
</exclusions>
Parthenopaeus answered 7/8, 2017 at 14:9 Comment(2)
My use case was the opposite, I wanted to use the Stax parser.Incision
I tested in jboss EAP 4.3 JDK 1.7 and also works fine.Donoho
C
1

The solution with the stax-api exclusion works fine in the use of the 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>
Crumpler answered 12/6, 2020 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.