如何防止javax变形金刚从空白空间? [英] How Do You Prevent A javax Transformer From Escaping Whitespace?
问题描述
我正在使用javax.xml.transform.Transformer类执行一些XSLT转换,如下所示:
I'm using the javax.xml.transform.Transformer class to perform some XSLT translations, like so:
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());
然而,无论我做什么,似乎都不能避免变压器转换任何标签在源文档中与他们的字符实体相当(	
)。我已经尝试过:
However, no matter what I do I can't seem to avoid having the transformer convert any tabs that were in the source document in to their character entity equivalent (	
). I have tried both:
transformer.setParameter("encoding", "UTF-8");
和:
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
但这些都没有帮助。有没有人有什么建议?因为:
but neither of those help. Does anyone have any suggestions? Because:
					<MyElement>
看起来很愚蠢(即使它工作)。
looks really stupid (even if it does work).
推荐答案
所以这个答案竟然是很跛脚的:更新Xalan。我不知道我的旧版本有什么问题,但是当我切换到最新版本的时候:
http://xml.apache.org/xalan-j/downloads.html
突然,标签的实体转义已经消失了。感谢大家所有的帮助。
So the answer to this one turned out to be pretty lame: update Xalan. I don't know what was wrong with my old version, but when I switched to the latest version at: http://xml.apache.org/xalan-j/downloads.html suddenly the entity-escaping of tabs just went away. Thanks everyone for all your help though.
这篇关于如何防止javax变形金刚从空白空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!