概述
I want to create a XML where blanks are replaced by . But the Java-Transformer escapes the Ampersand, so that the output is  
Here is my sample code:
public class Test {
public static void main(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element element = document.createElement("element");
element.setTextContent(" ");
document.appendChild(element);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamResult streamResult = new StreamResult(stream);
transformer.transform(new DOMSource(document), streamResult);
System.out.println(stream.toString());
}
}
And this is the output of my sample code:
 
Any ideas to fix or avoid that? thanks a lot!
解决方案
Set the text content directly to the character you want, and the serializer will escape it for you if necessary:
element.setTextContent("u00A0");
最后
以上就是含蓄棒棒糖为你收集整理的java xml特殊字符转义,如何在Java-XML中禁用/避免和符号转义?的全部内容,希望文章能够帮你解决java xml特殊字符转义,如何在Java-XML中禁用/避免和符号转义?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复