【发布时间】:2015-08-05 05:56:26
【问题描述】:
我有一个要写入 xml 的对象。 xml 有转义字符,如“&”、“
我有这样的输入 xml
<?xml version="1.0" encoding="UTF-8"?>
<personName><firstName>Sam & Pat </firstName>
<sal> > than 10000 </sal>
</personName>
并且预期的xml应该是`
<?xml version="1.0" encoding="UTF-8"?>
<personName><firstName>Sam & Pat </firstName>
<sal> < than 10000 </sal>
</personName>
如果我使用 StringUtils,它会像这样转换所有“
<sal> < than 10000 </sal>
编辑:我实际上不能使用 JaxB。我正在使用 FreeMarkerTemplate 来执行此操作。这是代码。
File tempFile = File.createTempFile(fileName, ".tmp");
try (FileWriter writer = new FileWriter(tempFile)) {
freeMarkerConfig.setOutputEncoding(UTF_8);
Template template = freeMarkerConfig.getTemplate(templateName);
template.process(data, writer);
} `
The resultant file which get created should have the handled escape characters.
【问题讨论】:
-
添加一个您想要实现的具体示例。输入和输出。请阅读stackoverflow.com/help/how-to-ask
-
嗨,我已经添加了示例。
-
IMO 阅读完此线程后,我相信您的来源应该负责发送有效的 XML 文件。您可以根据需要对数据进行编码、转义,但您不必担心有效的 XML 结构。