Transforming a StAX Source in Java
Asked Answered
A

2

8

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

Alburg answered 29/5, 2013 at 12:6 Comment(1)
So the problem here is that I should be actually using a TransformerFactory of type org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl. Unfortunately, Eclipse doesn't seem to provide that for me. It's also worth noting analogous code outside Eclipse works as expected.Alburg
A
6

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");
Alburg answered 3/6, 2013 at 20:39 Comment(0)
B
0

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()
Bullate answered 17/2, 2022 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.