Assuming this input XML
<?xml version="1.0" encoding="UTF-16"?>
<test></test>
Writing these lines of code :
StreamSource source = new StreamSource(new StringReader(/* the above XML*/));
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
TransformerFactory.newInstance().newTransformer().transform(source, streamResult);
return stringWriter.getBuffer().toString();
Outputs for me this XML:
<?xml version="1.0" encoding="UTF-8"?>
<test></test>
(the declared encoding of UTF-16 is converted to the default UTF-8)
I know I can explicitly ask for UTF-16 output
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-16");
But the question is, how to make the output encoding automatically be the same as the input?
try this:
The XSLT processor doesn't actually know what the input encoding is (the XML parser doesn't tell it, because it doesn't need to know). You can set the output encoding using xsl:output, but to make this the same as the input encoding you're going to have to discover the input encoding first, for example by peeking at the source file before parsing it.
You need to peek into the stream first. Section F of the XML specification gives you an idea how to auto-detect the encoding.
To do this, you'll have to use something more sophisticated than a
StreamSource
. For example, aStAXSource
takes anXMLStreamReader
, which has thegetCharacterEncodingScheme()
method that tells you which encoding the input document used - you can the set that as output enocding.