javaxml-parsingjaxbjava-8jaxb2

Any Alternate Solution to Xml Unmarshalling Error in Java-8? : “secure-processing org.xml.sax.SAXNotRecognizedException"


Note: This error has already been discussed here and various solutions also have been suggested to get rid of jars that conflicts with java-8's default implementation.

My question is an extension to that problem.

For the sake of completeness, here is the stacktrace that i am also facing, when i run the code with java-8:

Feb 29, 2016 12:06:41 PM com.sun.xml.internal.bind.v2.util.XmlFactory createParserFactory
SEVERE: null
org.xml.sax.SAXNotRecognizedException: http://javax.xml.XMLConstants/feature/secure-processing
    at org.apache.xerces.parsers.AbstractSAXParser.setFeature(AbstractSAXParser.java:1487)
    at org.apache.xerces.jaxp.SAXParserImpl.setFeatures(SAXParserImpl.java:145)
    at org.apache.xerces.jaxp.SAXParserImpl.<init>(SAXParserImpl.java:128)
    at org.apache.xerces.jaxp.SAXParserFactoryImpl.newSAXParserImpl(SAXParserFactoryImpl.java:112)
    at org.apache.xerces.jaxp.SAXParserFactoryImpl.setFeature(SAXParserFactoryImpl.java:140)
    at com.sun.xml.internal.bind.v2.util.XmlFactory.createParserFactory(XmlFactory.java:121)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.getXMLReader(UnmarshallerImpl.java:139)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:276)

To prove that conflicting jar(s) are indeed the problem (as described in the link shared earlier), I did pull out the relevant code into a different project and successfully ran the code using java-8 (i.e. without any errors) with bare minimum number of jars. So far so good.

And now the query:

In my case, my legacy project uses about 2-3 known conflicting jars used for various reasons. However, there is this new module which uses annotation driven jaxb for oxm processing.

The problem is : I can not get rid of those old and conflicting jars as that pretty much makes 10% of the codebase uncompilable. However, at the same time, i do not wish to get rid of the jaxb/oxm implementation in the new module as well.

Is there a way by which i can somehow tell JVM to ignore old jars and to use the default implementation that is shipped with java-8 whenever the code execution goes through this new module?


Solution

  • An alternative approach could be to use the StAX parser to get the XML as an XMLStreamReader and then have JAXB unmarshal that.

    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);
    unmarshaller.unmarshal(xmlStreamReader);