gpt4 book ai didi

java - XML 字符编码

转载 作者:行者123 更新时间:2023-11-30 04:53:29 26 4
gpt4 key购买 nike

我正在基于这样的字符串生成 XML:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();

// Use String reader
Document document = builder.parse(new InputSource(new StringReader(txt)));

TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(document);
Result dest = new StreamResult(new File("C:\\Users\\root\\Desktop\\xmlFileName.xml"));
aTransformer.transform(src, dest);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

它生成文件,但我遇到字符问题。

例如,生成的 XML 中的这一行:

<msg_detalhe>NÃ&#131;£o Ã&#131;© permitida a presenÃ&#131;§a de caracteres</msg_detalhe>

应该是:

<msg_detalhe>Não é permitida a presença de caracteres</msg_detalhe>

最佳答案

在 XML 内容中使用 CDATA...

<![CDATA[content]]>

关于java - XML 字符编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9327236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com