我使用dom4j在Java中创建XML字符串。存在诸如“年龄> 10且数量<11”的句子 我执行以下代码
String wholeText = "the age > 10 and number < 11.";
Element text = section.addElement("text");
text.addText(wholeText);
当我显示文字内容时,它被转换为
"the age > 10 and number < 11."
“&lt;”和“&gt;”符号已转换为html字符串 使用dom4j将文本添加到XML标记中时,是否有任何方法可以保留原始符号? 非常感谢
答案 0 :(得分:3)
除非您将内容包装在CDATA部分中,否则某些字符(尤其是<
)必须以这种方式进行转义。
要将其作为dom4j的CDATA部分,您可以使用
String wholeText = "the age > 10 and number < 11.";
Element text = section.addElement("text");
text.addCDATA(wholeText);