Search code examples
xmldom4j

keep the original symbol when adding text into XML tag using dom4j


I am using the dom4j to create XML string in Java. There is a sentence such as "the age > 10 and number < 11."
I do the following code

String wholeText = "the age > 10 and number < 11.";
Element text = section.addElement("text");  
text.addText(wholeText);

when I show the text content, it was converted as

"the age &gt; 10 and number &lt; 11."

the "<" and ">" symbol was converted to the html string.
Does any way to keep the original symbol when I add text into XML tag using dom4j?
thanks a lot


Solution

  • Some characters (especially <) have to be escaped this way unless you wrap the content in a CDATA section.

    To make it a CDATA section from dom4j, you could use

    String wholeText = "the age > 10 and number < 11.";
    Element text = section.addElement("text");  
    text.addCDATA(wholeText);