How to disable escaping using XMLOutputter

你说的曾经没有我的故事 提交于 2019-12-10 16:59:22

问题


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>

is written as

<string name="sname">&lt;u&gt;Text&lt;u&gt;</string>

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 &lt; there)


回答1:


You should be able to prevent escaping by:

  • Extending XMLOutputer and overriding escapeElementEntities method

    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();
    

I found this here

  • Setting custom EscapeStrategy on the Format to prevent escaping certain characters

Either way you should be careful to produce a valid XML.

Hope this helps.



来源:https://stackoverflow.com/questions/15359198/how-to-disable-escaping-using-xmloutputter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!