本文介绍了如何使用 XMLOutputter 禁用转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 XMLOutputter 编写 xml 文件然后在 android 中使用它.写入文件时,字符串
I use XMLOutputter to write xml file to use it then in android. When the file is written, string
<string name="sname"><u>Text</u></string>
写成
<string name="sname"><u>Text<u></string>
我读到文本"从数据库中,然后将其放入jdom文件.然后我使用
I read "<u>Text<u>" from database and then put it into jdom Document. Then I write the document using
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
Writer writer = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
outputter.output(doc, writer);
writer.close();
如何防止转义并将确切的字符串从数据库放入文件?(我想要'<',而不是<
)
How can I prevent escaping and put the exact string from database to file? (I want to have '<', not <
there)
推荐答案
您应该能够通过以下方式防止转义:
You should be able to prevent escaping by:
扩展
XMLOutputer
并覆盖escapeElementEntities
方法
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()) {
@Override
public String escapeElementEntities(String str) {
return str;
}
};
Writer writer = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
outputter.output(doc, writer);
writer.close();
我在这里
- 在
Format
上设置自定义EscapeStrategy
以防止转义某些字符
- Setting custom
EscapeStrategy
on theFormat
to prevent escaping certain characters
无论哪种方式,您都应该小心生成有效的 XML.
Either way you should be careful to produce a valid XML.
希望这会有所帮助.
这篇关于如何使用 XMLOutputter 禁用转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!