How to disable/avoid Ampersand-Escaping in Java-XML?

后端 未结 4 1085
梦谈多话
梦谈多话 2021-01-05 10:50

I want to create a XML where blanks are replaced by  . But the Java-Transformer escapes the Ampersand, so that the output is  

4条回答
  •  误落风尘
    2021-01-05 11:24

    The solution is very funny:

    Node disableEscaping = document.createProcessingInstruction(StreamResult.PI_DISABLE_OUTPUT_ESCAPING, "&");
     Element element = document.createElement("element");
     element.setTextContent(" ");
     document.appendChild(disableEscaping );
     document.appendChild(element);
    Node enableEscaping = document.createProcessingInstruction(StreamResult.PI_ENABLE_OUTPUT_ESCAPING, "&");
    document.appendChild(enableEscaping )
    

    So basically you need put your code between escaping element.

提交回复
热议问题