java - 使用 Jackson 库将 Java Map 转换为 XML
问题描述
如何Map
使用 Jackson 将 Java 转换为 XML?
版本:
'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.8'
代码:
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class MainApp {
public static void main(String[] args) throws JsonProcessingException {
Map<String, Object> map = new HashMap<String, Object>();
map.put("key1", "value1");
map.put("key2", "value2");
Application app = new Application();
app.setEntry(map);
// xml output format
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
System.out.println(xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(app));
}
@JacksonXmlRootElement(localName = "headers")
public static class Application {
private Map<String, Object> entry;
public Map<String, Object> getEntry() {
return Collections.unmodifiableMap(entry);
}
public void setEntry(Map<String, Object> entry) {
this.entry = entry;
}
}
}
实际输出:
<?xml version='1.0' encoding='UTF-8'?>
<headers>
<entry>
<key1>value1</key1>
<key2>value2</key2>
</entry>
</headers>
期望的输出:
<?xml version='1.0' encoding='UTF-8'?>
<headers>
<entry key="key1">value1</entry>
<entry key="key2">value2</entry>
</headers>
解决方案
您需要实现自定义序列化程序来编写Map
这样的条目。示例实现:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class XmlApp {
public static void main(String[] args) throws IOException {
Map<String, Object> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
Application app = new Application();
app.setEntry(map);
// xml output format
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
System.out.println(xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(app));
}
}
@JacksonXmlRootElement(localName = "headers")
@JsonSerialize(using = ApplicationJsonSerializer.class)
class Application {
private Map<String, Object> entry;
public Map<String, Object> getEntry() {
return Collections.unmodifiableMap(entry);
}
public void setEntry(Map<String, Object> entry) {
this.entry = entry;
}
}
class ApplicationJsonSerializer extends JsonSerializer<Application> {
@Override
public void serialize(Application value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
ToXmlGenerator xmlGen = (ToXmlGenerator) gen;
xmlGen.writeStartObject();
for (Map.Entry<String, Object> entry : value.getEntry().entrySet()) {
xmlGen.writeObjectFieldStart("entry");
writeAttributes(xmlGen, entry.getKey());
xmlGen.writeRaw(entry.getValue().toString());
xmlGen.writeEndObject();
}
xmlGen.writeEndObject();
}
private void writeAttributes(ToXmlGenerator gen, String key) throws IOException {
gen.setNextIsAttribute(true);
gen.writeFieldName("key");
gen.writeString(key);
gen.setNextIsAttribute(false);
}
}
上面的代码打印:
<?xml version='1.0' encoding='UTF-8'?>
<headers>
<entry key="key1">value1</entry>
<entry key="key2">value2</entry>
</headers>
推荐阅读
- aws-step-functions - AWS Step Function 如何使用 dynamodb:getItem 获取整数?
- apache-camel - 为什么 3.x Camel 在启动时失败?
- html - 使用“表单操作”使用变量填充“mailto:”值
- python - 如何扩展图表上的线性回归线?
- angular - Keycloak 登录页面 URL
- node.js - Pm2 启动进程但节点应用程序无法访问
- rust - 如何使用 Rust 的解析器库 nom 正确解析这个程序
- ios - iOS - 发布模式下 xcode 上的 Flutter audioplayers 插件错误(未找到)
- alexa - 如何从外部呼叫 Alexa?
- sql - Oracle SQL 中具有相同列名的多个表连接