将JSON转换为XML XmlMapper- Jackson 2.10 [英] Converting JSON to XML XmlMapper- Jackson 2.10

查看:835
本文介绍了将JSON转换为XML XmlMapper- Jackson 2.10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有@JsonProperties的pojos.我用它们读取JSON并解析为POJO.我现在必须发布这些格式为XML的pojos.

I have pojos with @JsonProperties. I use these to read JSON and parse to POJO. I am now having to post these pojos formatted as XML.

成功发布所需的XML格式如下(请注意,命名空间类型,xsi类型格式):

The Required XML format to successfully post looks like this (note the namespace type, xsi type formatting):

<network_objects>
    <network_object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="subnetNetworkObjectDTO">
        <name>TestSubnet</name>
        <display_name>TestSubnet</display_name>
        <global>false</global>
        <application_id>3</application_id>
        <type>subnet</type>
        <ip>5.207.206.0</ip>
        <netmask>255.255.254.0</netmask>
    </network_object>
    <network_object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="networkObjectGroupDTO">
        <name>Test01Subnets</name>
        <display_name>Test01Subnets</display_name>
        <application_id>3</application_id>
        <type>group</type>
    </network_object>
</network_objects>

我将创建的应用程序类(如下所示)传递给XML

I pass the created Application Class (shown below) to convert to XML

    ObjectMapper mapper = new XmlMapper(); 
    mapper.enable(SerializationFeature.INDENT_OUTPUT);        
    byte[] val = mapper.writeValueAsBytes(myApp);

输出有点不正确,不包含xmlns,xsi看起来也有所不同.它还以应用程序"为根:

The output is a bit off and does not contain the xmlns and the xsi looks different. It also has 'Application' as root:

<Application>
 <network_objects>
  <network_object>
   <network_object>
    <@xsi.type>networkObjectGroupDTO</@xsi.type>
    <name>name</name>
    <display_name>displayName</display_name>
    <application_id>3</application_id>
    <type>group</type>
   </network_object>
  </network_object>
 </network_objects>
</Application>

当我将类输出为JSON时,它看起来像预期的那样(没有"Application"作为根).

When I output the class to JSON, it looks as expected (No "Application" as root).

    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);        
    byte[] val = mapper.writeValueAsBytes(myApp);


{
"network_objects" : {
"network_object" : [ {
  "@xsi.type" : "networkObjectGroupDTO",
  "name" : "name",
  "display_name" : "displayName",
  "application_id" : 3,
  "type" : "group"
   }
  }
}

我需要用XmlMapper()或POJO进行哪些修改才能正确格式化XML?

What do I need to modify with my XmlMapper() or POJOs in order to get the XML formatted correctly?

下面是用于此的POJO.

Below are the POJOs used for this.

应用程序类:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"network_objects"
})
public class Application {

@JsonProperty("network_objects")
private NetworkObjects networkObjects;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("network_objects")
public NetworkObjects getNetworkObjects() {
    return networkObjects;
}

@JsonProperty("network_objects")
public void setNetworkObjects(NetworkObjects networkObjects) {
    this.networkObjects = networkObjects;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

}

NetworkObjects类:

NetworkObjects Class:

@JsonInclude(JsonInclude.Include.NON_NULL)
 @JsonPropertyOrder({
"network_object"
 })
 public class NetworkObjects {

@JsonProperty("network_object")
private List<NetworkObject> networkObject = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("network_object")
public List<NetworkObject> getNetworkObject() {
    return networkObject;
}

@JsonProperty("network_object")
public void setNetworkObject(List<NetworkObject> networkObject) {
    this.networkObject = networkObject;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

 }

NetworkObject类:

NetworkObject Class:

@JsonInclude(JsonInclude.Include.NON_NULL)
 @JsonPropertyOrder({
"@xsi.type",
"id",
"uid",
"name",
"display_name",
"global",
"comment",
"application_id",
"type",
"ip",
"access_allowed",
"member",
"last_ip",
"first_ip",
"netmask"
 })
 public class NetworkObject {

@JsonProperty("@xsi.type")
private String xsiType;
@JsonProperty("id")
private Integer id;
@JsonProperty("uid")
private String uid;
@JsonProperty("name")
private String name;
@JsonProperty("display_name")
private String displayName;
@JsonProperty("global")
private Boolean global;
@JsonProperty("comment")
private String comment;
@JsonProperty("application_id")
private Integer applicationId;
@JsonProperty("type")
private String type;
@JsonProperty("ip")
private String ip;
@JsonProperty("access_allowed")
private Boolean accessAllowed;
@JsonProperty("member")
private List<Member> member = null;
@JsonProperty("last_ip")
private String lastIp;
@JsonProperty("first_ip")
private String firstIp;
@JsonProperty("netmask")
private String netmask;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("@xsi.type")
public String getXsiType() {
    return xsiType;
}

@JsonProperty("@xsi.type")
public void setXsiType(String xsiType) {
    this.xsiType = xsiType;
}

@JsonProperty("id")
public Integer getId() {
    return id;
}

@JsonProperty("id")
public void setId(Integer id) {
    this.id = id;
}

@JsonProperty("uid")
public String getUid() {
    return uid;
}

@JsonProperty("uid")
public void setUid(String uid) {
    this.uid = uid;
}

@JsonProperty("name")
public String getName() {
    return name;
}

@JsonProperty("name")
public void setName(String name) {
    this.name = name;
}

@JsonProperty("display_name")
public String getDisplayName() {
    return displayName;
}

@JsonProperty("display_name")
public void setDisplayName(String displayName) {
    this.displayName = displayName;
}

@JsonProperty("global")
public Boolean getGlobal() {
    return global;
}

@JsonProperty("global")
public void setGlobal(Boolean global) {
    this.global = global;
}

@JsonProperty("comment")
public String getComment() {
    return comment;
}

@JsonProperty("comment")
public void setComment(String comment) {
    this.comment = comment;
}

@JsonProperty("application_id")
public Integer getApplicationId() {
    return applicationId;
}

@JsonProperty("application_id")
public void setApplicationId(Integer applicationId) {
    this.applicationId = applicationId;
}

@JsonProperty("type")
public String getType() {
    return type;
}

@JsonProperty("type")
public void setType(String type) {
    this.type = type;
}

@JsonProperty("ip")
public String getIp() {
    return ip;
}

@JsonProperty("ip")
public void setIp(String ip) {
    this.ip = ip;
}

@JsonProperty("access_allowed")
public Boolean getAccessAllowed() {
    return accessAllowed;
}

@JsonProperty("access_allowed")
public void setAccessAllowed(Boolean accessAllowed) {
    this.accessAllowed = accessAllowed;
}

@JsonProperty("member")
public List<Member> getMember() {
    return member;
}

@JsonProperty("member")
public void setMember(List<Member> member) {
    this.member = member;
}

@JsonProperty("last_ip")
public String getLastIp() {
    return lastIp;
}

@JsonProperty("last_ip")
public void setLastIp(String lastIp) {
    this.lastIp = lastIp;
}

@JsonProperty("first_ip")
public String getFirstIp() {
    return firstIp;
}

@JsonProperty("first_ip")
public void setFirstIp(String firstIp) {
    this.firstIp = firstIp;
}

@JsonProperty("netmask")
public String getNetmask() {
    return netmask;
}

@JsonProperty("netmask")
public void setNetmask(String netmask) {
    this.netmask = netmask;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

 }

推荐答案

我使用了您的bean类和json字符串.转换几乎可以实现所有功能,但是存在一些问题.到目前为止,我已经明白了,如果有人可以得出最终的解决方案,那就太好了.

I used your bean classes and json string. Almost everything is possible with the conversion, but there are some issues. I got this so far, if someone can derive the final solution it will be great.

Application.java

实际上没有任何变化.

NetworkObject.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"@xsi.type", "id", "uid", "name", "display_name", "global", "comment", "application_id", "type", "ip", "access_allowed", "member", "last_ip", "first_ip", "netmask"})
public class NetworkObject {

    @JsonProperty("id")
    private Integer id;
    @JsonProperty("uid")
    private String uid;
    @JsonProperty("name")
    private String name;
    @JsonProperty("display_name")
    private String displayName;
    @JsonProperty("global")
    private Boolean global;
    @JsonProperty("comment")
    private String comment;
    @JsonProperty("application_id")
    private Integer applicationId;
    @JsonProperty("type")
    private String type;
    @JsonProperty("@xsi.type")
    // CHANGE: You cannot have same element name and attribute name, so I had to change this to xtype if someone knows how to tackle this, that will be final answer
    @JacksonXmlProperty(localName = "xtype", isAttribute = true, namespace = "http://www.w3.org/2001/XMLSchema-instance")
    private String xsiType;
    @JsonProperty("ip")
    private String ip;
    @JsonProperty("access_allowed")
    private Boolean accessAllowed;
    @JsonProperty("member")
    private List<Member> member = null;
    @JsonProperty("last_ip")
    private String lastIp;
    @JsonProperty("first_ip")
    private String firstIp;
    @JsonProperty("netmask")
    private String netmask;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    // CHANGE: We don't really need on getter setters @JsonProperty("@xsi.type")
    public String getXsiType() {
        return xsiType;
    }

    // CHANGE: We don't really need on getter setters @JsonProperty("@xsi.type")
    public void setXsiType(String xsiType) {
        this.xsiType = xsiType;
    }

    @JsonProperty("id")
    public Integer getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(Integer id) {
        this.id = id;
    }

    @JsonProperty("uid")
    public String getUid() {
        return uid;
    }

    @JsonProperty("uid")
    public void setUid(String uid) {
        this.uid = uid;
    }

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("display_name")
    public String getDisplayName() {
        return displayName;
    }

    @JsonProperty("display_name")
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    @JsonProperty("global")
    public Boolean getGlobal() {
        return global;
    }

    @JsonProperty("global")
    public void setGlobal(Boolean global) {
        this.global = global;
    }

    @JsonProperty("comment")
    public String getComment() {
        return comment;
    }

    @JsonProperty("comment")
    public void setComment(String comment) {
        this.comment = comment;
    }

    @JsonProperty("application_id")
    public Integer getApplicationId() {
        return applicationId;
    }

    @JsonProperty("application_id")
    public void setApplicationId(Integer applicationId) {
        this.applicationId = applicationId;
    }

    @JsonProperty("type")
    public String getType() {
        return type;
    }

    @JsonProperty("type")
    public void setType(String type) {
        this.type = type;
    }

    @JsonProperty("ip")
    public String getIp() {
        return ip;
    }

    @JsonProperty("ip")
    public void setIp(String ip) {
        this.ip = ip;
    }

    @JsonProperty("access_allowed")
    public Boolean getAccessAllowed() {
        return accessAllowed;
    }

    @JsonProperty("access_allowed")
    public void setAccessAllowed(Boolean accessAllowed) {
        this.accessAllowed = accessAllowed;
    }

    @JsonProperty("member")
    public List<Member> getMember() {
        return member;
    }

    @JsonProperty("member")
    public void setMember(List<Member> member) {
        this.member = member;
    }

    @JsonProperty("last_ip")
    public String getLastIp() {
        return lastIp;
    }

    @JsonProperty("last_ip")
    public void setLastIp(String lastIp) {
        this.lastIp = lastIp;
    }

    @JsonProperty("first_ip")
    public String getFirstIp() {
        return firstIp;
    }

    @JsonProperty("first_ip")
    public void setFirstIp(String firstIp) {
        this.firstIp = firstIp;
    }

    @JsonProperty("netmask")
    public String getNetmask() {
        return netmask;
    }

    @JsonProperty("netmask")
    public void setNetmask(String netmask) {
        this.netmask = netmask;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

NetworkObjects

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"network_object"})
// CHANGE: To provide root element name
@JacksonXmlRootElement(localName = "network_objects")
public class NetworkObjects {

    @JsonProperty("network_object")
    @JacksonXmlElementWrapper(useWrapping = false)
    // CHANGE: To ignore <network_object><network_object></network_object></network_object>
    private List<NetworkObject> networkObject = null;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("network_object")
    public List<NetworkObject> getNetworkObject() {
        return networkObject;
    }

    @JsonProperty("network_object")
    public void setNetworkObject(List<NetworkObject> networkObject) {
        this.networkObject = networkObject;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

Main.java

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper jsonOM = new ObjectMapper();
    String jsomn = Files.readAllLines(Paths.get("sample.json"), StandardCharsets.US_ASCII).stream().collect(Collectors.joining(""));
    Application myApp = jsonOM.readValue(jsomn, Application.class);

    ObjectMapper mapper = new XmlMapper(); 
    mapper.enable(SerializationFeature.INDENT_OUTPUT);    
    // Not writing entire object

  System.out.println(mapper.writer().writeValueAsString(myApp.getNetworkObjects()));
}

输出

<network_objects>
  <network_object xmlns:wstxns1="http://www.w3.org/2001/XMLSchema-instance" wstxns1:xtype="networkObjectGroupDTO">
    <name>name</name>
    <display_name>displayName</display_name>
    <application_id>3</application_id>
    <type>group</type>
  </network_object>
</network_objects>

这篇关于将JSON转换为XML XmlMapper- Jackson 2.10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆