문제

I have some code like:

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)

Which produces the following error:

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

The reader object is of type 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");

다른 팁

I stumbled upon Xml Transformer give me an error when trying to transform a StaxSource into a StreamResult, while looking to solve the very same issue as you.

The answer provided there seems to be working fine for me, i.e. use:

TransformerFactory.newDefaultInstance();

Instead of:

 XMLInputFactory.newInstance()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top