【发布时间】:2018-04-04 06:53:57
【问题描述】:
我有一个函数,它将 XML 文档作为参数并将其写入文件。它包含<tag>"some text & some text": <text> text</tag> 的元素,但在输出文件中它写为<tag>"some text &amp; some text": &lt;text&gt; text</tag> 但我不希望在写入文件时字符串被转义。
函数是,
public static void function(Document doc, String fileUri, String randomId){
DOMSource source = new DOMSource(doc,ApplicationConstants.ENC_UTF_8);
FileWriterWithEncoding writer = null;
try {
File file = new File(fileUri+File.separator+randomId+".xml");
if (!new File(fileUri).exists()){
new File(fileUri).mkdirs();
}
writer = new FileWriterWithEncoding(new File(file.toString()),ApplicationConstants.ENC_UTF_8);
StreamResult result = new StreamResult(writer);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
transformer = transformerFactory.newTransformer();
transformer.setParameter(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
writer.close();
transformer.clearParameters();
}catch (IOException | TransformerException e) {
log.error("convert Exception is :"+ e);
}
}
【问题讨论】:
-
您是否意识到,如果您可以按照您的要求进行操作,您的输出将不再是 XML,并且不再被 XML 解析器读回?那是你要的吗?如果是,为什么?
-
我只希望元素的文本不要被转义