简体   繁体   English

将带有转义双引号的字符串序列化为 XML

[英]Serializing string with escaped double quotes to XML

I have a problem trying to serialize a Java Object to XML using jackson XmlMapper.我在尝试使用 jackson XmlMapper 将 Java 对象序列化为 XML 时遇到问题。

One of the fields contains a string with escaped double quotes, like this:其中一个字段包含一个带有转义双引号的字符串,如下所示:

String example = "This is a string with \\"escaped double quotes\\"";

When i pass object to XMLmapper (variables is the object containing example string):当我将对象传递给 XMLmapper 时(变量是包含示例字符串的对象):

  XmlMapper xmlMapper = new XmlMapper();
  String xml = xmlMapper.writeValueAsString(variables);

this is the output i get:这是我得到的输出:

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

How can i serialize a string with escaped doubled quotes to XML achieving proper result, ie我如何将带有转义双引号的字符串序列化为 XML 以获得正确的结果,即

<example>This is a string with "escaped double quotes"</example>

Then: "Sorry, cannot reproduce"!然后:“对不起,无法复制”!

pom.xml: 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: 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: 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);
    }
}

... >mvn clean package exec:java -Dexec.mainClass=com.example.xml.mapper.Main (jdk8), prints: ... >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
------------------------------------------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM