有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Transformer在XML中取消显示字符串?

我有一个函数,它将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);
    }
}

共 (1) 个答案