【问题标题】:Serializing string with escaped double quotes to XML将带有转义双引号的字符串序列化为 XML
【发布时间】: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);):&lt;SimpleBean&gt;&lt;example&gt;This is a string with "escaped double quotes"&lt;/example&gt;&lt;/SimpleBean&gt;
  • @xerx593 我在控制台打印出来了,和你一样。
  • 您确定您自己没有尝试以某种方式转义字符串吗? (没有必要这样做。)
  • @VGR 不,字符串是从收到的 JSON 中解析出来的,在调用 xmlMapper 之前,它看起来与我的问题中的“示例”变量完全相同。

标签: java jackson


【解决方案1】:

然后:“抱歉,无法重现”!

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>xml-mapper</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.13.0</version> <!-- tried [2.11.0 - 2.13.0], same result -->
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
    </dependencies>
</project>

src/main/java/com/example/xml/mapper/SimpleBean.java:

package com.example.xml.mapper;

import lombok.Data;

@Data
public class SimpleBean {
    final String example = "This is a string with \"escaped double quotes\"";
}

src/main/java/com/example/xml/mapper/Main.java:

package com.example.xml.mapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class Main {

    public static void main(String[] args) throws JsonProcessingException  {
        XmlMapper xmlMapper = new XmlMapper();
        String xml = xmlMapper.writeValueAsString(new SimpleBean());
        System.out.println(xml);
    }
}

...&gt;mvn clean package exec:java -Dexec.mainClass=com.example.xml.mapper.Main (jdk8),打印:

...

--- exec-maven-plugin:3.0.0:exec (default-cli) @ xml-mapper ---
<SimpleBean><example>This is a string with "escaped double quotes"</example></SimpleBean>
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  1.570 s
Finished at: 2021-11-16T13:02:33+01:00
------------------------------------------------------------------------

【讨论】:

  • ..也没有变化(像魅力;-(),在“可变”bean上......
猜你喜欢
  • 2015-10-27
  • 2015-12-02
  • 1970-01-01
  • 2017-08-21
  • 2020-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多