简体   繁体   English

Java-Jackson 序列化 ArrayList<string> 到具有不同子名的 XML</string>

[英]Java-Jackson Serialize ArrayList<String> to XML with different child names

I've been using jackson-dataformatter-v2.9.10 to serialize a Java class into an XML string.我一直在使用 jackson-dataformatter-v2.9.10 将 Java class 序列化为 XML 字符串。

This is my class:这是我的 class:

public class parent()
{
    public ArrayList<String> children;

    public parent() {
       
         children = new ArrayList<String>();
    }
}

Here is what I want to achieve:这是我想要实现的目标:

<parent>
    <children>
      <child>John</child>
      <child>Ben</child>
      <child>Mary</child>
    </children>
</parent>

Here is what I'm getting:这是我得到的:

<parent>
    <children>
      <children>John</children>
      <children>Ben</children>
      <children>Mary</children>
    </children>
</parent>

Any suggestion on how to change the ArrayList children element names ?关于如何更改 ArrayList子元素名称的任何建议?

Use both @JacksonXmlElementWrapper and @JacksonXmlProperty on the list field.在列表字段上同时使用@JacksonXmlElementWrapper@JacksonXmlProperty

Here is some sample code:这是一些示例代码:

    @JacksonXmlElementWrapper(localName = "children")
    @JacksonXmlProperty(localName = "child")
    private List<String> child = new LinkedList<>();

The ElementWrapper is for the outer element (the one that contains the repeating elements). ElementWrapper 用于外部元素(包含重复元素的元素)。 The XmlProperty is for the inner element (the one that repeats). XmlProperty 用于内部元素(重复的元素)。

You should be able to use你应该可以使用

@JacksonXmlElementWrapper(localName = "children")
List<String> child;

https://stackify.com/java-xml-jackson https://stackify.com/java-xml-jackson

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

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