【发布时间】:2021-11-16 11:23:24
【问题描述】:
我在尝试使用 jackson XmlMapper 将 Java 对象序列化为 XML 时遇到问题。
其中一个字段包含带有转义双引号的字符串,如下所示:
String example = "This is a string with \"escaped double quotes\"";
当我将对象传递给 XMLmapper (变量是包含示例字符串的对象):
XmlMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writeValueAsString(variables);
这是我得到的输出:
some other tags
<example>This is a string with "escaped double quotes""This is a string with "escaped double quotes""This is a string with "escaped double quotes"</example>
some other tags
如何将带有转义双引号的字符串序列化为 XML 以获得正确的结果,即
<example>This is a string with "escaped double quotes"</example>
【问题讨论】:
-
嗨,欢迎! ;) 你在哪里“得到输出”? (抱歉,无法重现:我得到了输出(来自
System.out.println(xml);):<SimpleBean><example>This is a string with "escaped double quotes"</example></SimpleBean>) -
@xerx593 我在控制台打印出来了,和你一样。
-
您确定您自己没有尝试以某种方式转义字符串吗? (没有必要这样做。)
-
@VGR 不,字符串是从收到的 JSON 中解析出来的,在调用 xmlMapper 之前,它看起来与我的问题中的“示例”变量完全相同。