所有
我有以下代码将Source
转换为String
。这一直有效,直到我将jackson-dataformat-xml
添加到我的maven POM中。不幸的是,我确实需要这种依赖。
try {
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
final StringWriter writer = new StringWriter();
final StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
return writer.toString();
} catch (final Exception e) {
throw new IOException(e);
}
添加jackson-dataformat-xml
后,我收到以下异常
Caused by: java.lang.ClassCastException: org.codehaus.stax2.ri.evt.NamespaceEventImpl cannot be cast to java.lang.String
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.handleEndElement(StAXEvent2SAX.java:342) ~[?:1.8.0_51]
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.bridge(StAXEvent2SAX.java:199) ~[?:1.8.0_51]
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXEvent2SAX.parse(StAXEvent2SAX.java:116) ~[?:1.8.0_51]
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:688) ~[?:1.8.0_51]
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:743) ~[?:1.8.0_51]
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:357) ~[?:1.8.0_51]
[the-above-code]
我尝试过一些事情,比如手动设置TransformerFactory
,但没有运气。有什么想法吗?