英文:
Java-Jackson Serialize ArrayList<String> to XML with different child names
问题
我一直在使用jackson-dataformatter-v2.9.10将Java类序列化为XML字符串。
这是我的类:
public class parent()
{
    public ArrayList<String> children;
    public parent() {
        children = new ArrayList<String>();
    }
}
这是我想要实现的:
<parent>
    <children>
        <child>John</child>
        <child>Ben</child>
        <child>Mary</child>
    </children>
</parent>
这是我得到的:
<parent>
    <children>
        <children>John</children>
        <children>Ben</children>
        <children>Mary</children>
    </children>
</parent>
有关如何更改ArrayList children元素名称的建议吗?
英文:
I've been using jackson-dataformatter-v2.9.10 to serialize a Java class into an XML string.
This is my 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?
答案1
得分: 2
使用@JacksonXmlElementWrapper和@JacksonXmlProperty同时应用于列表字段。
以下是示例代码:
	@JacksonXmlElementWrapper(localName = "children")
	@JacksonXmlProperty(localName = "child")
	private List<String> child = new LinkedList<>();
ElementWrapper用于外部元素
(包含重复元素的元素)。
XmlProperty用于内部元素
(重复的元素)。
英文:
Use both @JacksonXmlElementWrapper and @JacksonXmlProperty on the list field.
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).
The XmlProperty is for the inner element
(the one that repeats).
答案2
得分: 0
你应该能够使用
@JacksonXmlElementWrapper(localName = "children")
List<String> child;
https://stackify.com/java-xml-jackson
英文:
You should be able to use
@JacksonXmlElementWrapper(localName = "children")
List<String> child;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论