1

I ham using JAXB for Unmarshalling and marshalling below is the code snippet

public static Application getApplicationFromString(String str) throws JAXBException, ParserConfigurationException, SAXException, IOException {
    JAXBContext jb = getJAXBContext();
    Unmarshaller um = jb.createUnmarshaller();
    Application app = (Application) um.unmarshal(IOUtils.toInputStream(str));
    return app;
}

private static JAXBContext getJAXBContext() throws JAXBException {      
    return JAXBContext.newInstance(Application.class);      
}

It works fine , but when i add a jar saxon9.jar it gives the exception. Not sure why this is happening.i need this jars for other modules.

Exception in thread "main" java.lang.UnsupportedOperationException: Saxon cannot write a DOMResult unless saxon9-dom.jar is on the classpath
    at net.sf.saxon.event.SerializerFactory.getReceiver(SerializerFactory.java:189)
    at net.sf.saxon.IdentityTransformerHandler.startDocument(IdentityTransformerHandler.java:99)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.DomLoader$State.<init>(DomLoader.java:68)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.DomLoader.startElement(DomLoader.java:103)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ProxyLoader.startElement(ProxyLoader.java:45)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:559)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204)
    at TestJaxB.getApplicationFromString(TestJaxB.java:46)
    at TestJaxB.main(TestJaxB.java:25)
Florent Georges
  • 2,190
  • 1
  • 15
  • 24
user2478236
  • 691
  • 12
  • 32

2 Answers2

2

It's an unfortunate consequence of the JAXP factory mechanism that when you put an XSLT processor on your classpath, software that was never designed or tested to run with that XSLT processor suddenly finds itself using it, which can have all sorts of unpredictable consequences. For this reason, recent releases of Saxon no longer advertise themselves as JAXP XPath factories, because the JAXP XPath API is so weakly defined that using a different engine from the one you tested with is very likely to cause your application to fail. The XSLT API, however, is less of a problem and most applications will use Saxon quite happily if it's available - often they will even get an unexpected performance boost. The particular failure here is because you have one of the Saxon JARs on your classpath and not the other. For a while (quite some years ago) Saxon separated the DOM support code into a separate JAR file because there was an incompatible change to the DOM API between JDK 1.4 and JDK 1.5 which made it very hard to produce a Saxon release that worked with both.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

Add saxon9-dom.jar to your classpath (from the same download you got the other Saxon JAR files). If I remember correctly, latest Saxon releases do not require a DOM JAR file anymore.

Florent Georges
  • 2,190
  • 1
  • 15
  • 24
  • But without the jar code works fine , when its added why does it throw the error.why is JAXB picking up classes for saxon9.jar – user2478236 Jun 23 '15 at 13:56
  • Why? Probably because of the complexity of deciding the default implementation for JAXP interfaces: http://stackoverflow.com/a/1804281/919264 – Florent Georges Jun 23 '15 at 14:00
  • I received this error after having multiple saxon dependencies listed in my maven project, including a version of saxon-dom. I resolved the error by only including Saxon-HE. – Bal Aug 25 '15 at 13:28