用Java转换StAX源代码

我有一些代码:

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)

这会产生以下错误:

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

reader对象的类型为com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl

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

System.setProperty("javax.xml.transform.TransformerFactory",
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
相关文章
相关标签/搜索