我正在使用 javax.xml.transform.Transformer 类来执行一些 XSLT 转换,如下所示:
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource source = new StreamSource(TRANSFORMER_PATH);
Transformer transformer = factory.newTransformer(source);
StringWriter extractionWriter = new StringWriter();
String xml = FileUtils.readFileToString(new File(sampleXmlPath));
transformer.transform(new StreamSource(new StringReader(xml)),
new StreamResult(extractionWriter));
System.err.println(extractionWriter.toString());
但是,无论我做什么,我似乎都无法避免让转换器将源文档中的任何选项卡转换为其等效的字符实体 ( 	
)。我都试过了:
transformer.setParameter("encoding", "UTF-8");
和:
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
但这些都没有帮助。有没有人有什么建议?因为:
					<MyElement>
看起来真的很愚蠢(即使它确实有效)。