简体   繁体   中英

Xml Transformer give me an error when trying to transform a StaxSource into a StreamResult

Trying to transform a Staxsource into a StreamResult give me an error (when running on Wildfly 24 using Adoptopenjdk 11-hotspot)

But the same code executed from an unit test pass without errors and do correctly the job (using Adoptopenjdk 11-hotspot)

Here the error I get :

14:50:15,455 ERROR [com.x.x.x.x.x.DeliveryParser] (default task-1) javax.xml.transform.TransformerException: Source object passed to ''{0}'' has no contents.
ERROR:  'Source object passed to ''{0}'' has no contents.'

Here the source code :

private static String readElementString(final XMLStreamReader streamReader)  {
    try {
        StringWriter stringWriter = new StringWriter();

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.toString());

        StAXSource stAXSource = new StAXSource(streamReader);
        StreamResult streamResult = new StreamResult(stringWriter);

        transformer.transform(stAXSource, streamResult);  <-- error is thrown here

        return stringWriter.toString();
        ...

Unable to find anthing I can use on the net.

Does someone have a hint for me ?

Thx - Fabien

Here the solution I found :

When on unit testing, the "TransformerFactory" give me an instance of "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl" which come from the JDK.

On Wildlfy, the factory give me an instance of "org.apache.xalan.xsltc.trax.TransformerImpl".

This last one cause me an error when I try to transform a StaxSource. I don't know why.

So, I'm now using this code below to instanciate the TransformerFactory from the JDK :

TransformerFactory.newDefaultInstance();

in place of

TransformerFactory.newInstance();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM