假定这一投入XML
<?xml version="1.0" encoding="UTF-16"?>
<test></test>
撰写这些法典:
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:
<?xml version="1.0" encoding="UTF-8"?>
<test></test>
(UTF-16的公开编码改为“UTF-8)
I know I can explicitly ask for UTF-16 output
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-16");
www.un.org/Depts/DGACM/index_spanish.htm 但问题是,如何使产出编码自动与投入相同?