Reputation: 1
I have a problem trying to serialize a Java Object to XML using jackson XmlMapper.
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 = 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, i.e.
<example>This is a string with "escaped double quotes"</example>
Upvotes: 0
Views: 590
Reputation: 13271
Then: "Sorry, cannot reproduce"!
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);
}
}
... >mvn clean package exec:java -Dexec.mainClass=com.example.xml.mapper.Main
(jdk8), prints:
...
--- 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
------------------------------------------------------------------------
Upvotes: 1