我使用JAXB进行Unmarshalling和marshalling下面是代码片段
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)
答案 0 :(得分:2)
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版本非常困难。
答案 1 :(得分:1)
将saxon9-dom.jar
添加到您的类路径中(从您获得其他Saxon JAR文件的同一下载中)。如果我没记错的话,最新的Saxon版本不再需要DOM JAR文件了。