Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StAX parser determination at runtime

Tags:

java

xml

jboss

stax

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 ?

like image 488
Lluis Martinez Avatar asked Nov 26 '14 11:11

Lluis Martinez


3 Answers

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.

like image 187
lexicore Avatar answered Oct 31 '22 22:10

lexicore


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>
like image 3
Md Islam Avatar answered Oct 31 '22 21:10

Md Islam


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>
like image 1
molino.bruno Avatar answered Oct 31 '22 21:10

molino.bruno