我火腿使用JAXB进行编组,下面的代码段是编组

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);
}


它工作正常,但是当我添加一个jar saxon9.jar时会给出异常。不知道为什么会这样。我需要其他模块的罐子。

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)

最佳答案

JAXP工厂机制的不幸结果是,当您在类路径上放置XSLT处理器时,从未设计过或未经测试可与该XSLT处理器一起运行的软件突然发现自己正在使用它,这可能会带来各种不可预测的后果。因此,Saxon的最新版本不再将自己宣传为JAXP XPath工厂,因为JAXP XPath API的定义太弱,以至于使用与您测试过的引擎不同的引擎很可能导致您的应用程序失败。但是,XSLT API的问题不大,如果可用,大多数应用程序将非常高兴地使用Saxon-通常它们甚至会获得意想不到的性能提升。此处的特定失败是因为在类路径中有一个Saxon JAR,而没有另一个。一段时间以来(很久以前),Saxon将DOM支持代码分离到一个单独的JAR文件中,因为JDK 1.4和JDK 1.5之间的DOM API发生了不兼容的更改,这使得很难制作同时兼容两者的Saxon版本。

关于java - 由于saxon9.jar而导致JAXB解码时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31000196/

10-12 06:10