在 Java 中转换 StAX 源

2022-01-10 00:00:00 xml-parsing java stax xalan

我有一些类似的代码:

XMLInputFactory xif = XMLInputFactory.newInstance()
TransformerFactory tf = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl", null)

Transformer t = tf.newTransformer()
DOMResult result = new DOMResult()
t.transform(new StAXSource(reader), result)

这会产生以下错误:

捕获:javax.xml.transform.TransformerException:无法转换 javax.xml.transform.stax.StAXSource 类型的源

Caught: javax.xml.transform.TransformerException: Can't transform a Source of type javax.xml.transform.stax.StAXSource

阅读器对象的类型是 com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl

推荐答案

所以我愚蠢地尝试了错误的事情.这修复了它:

So I was stupidly trying the wrong thing. This fixed it:

System.setProperty("javax.xml.transform.TransformerFactory",
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");

相关文章