当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Writing text to XML with XMLSerializer without escaping it

问题描述:

am trying to write text to an XML-File using the XMLSerializer. The text(String)-function escapes characters automatically.

Is it possible not to escape text when adding it to the xml-File?

If it is not possible, could you recommend an easy workaround without having to rewrite all of my XML creation code?

Thanks

网友答案:

You probably moved along and found your own solution in the meantime but here is a possible solution. Rather than using the serializer, I directly call the underlying StringWriter to insert raw text (in my case that is well formed xml, so I want to avoid escaping the <,> marks). Notice the serializer flush is very important to get the correct order.

XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
    serializer.setOutput(writer);
    serializer.startDocument("UTF-8", true);
    serializer.startTag(namespace, "destination");
    serializer.attribute(namespace, "id", String.valueOf(destinationId));
    serializer.flush();
    writer.write(DestinationContent);
    serializer.endTag(namespace, "destination");
    ...
} catch (Exception e) {
   throw new RuntimeException(e);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: