0

I need to create XML export from my java models using Jaxb annotations. What I need is the following xml in which is parent entity which contains multiple entities which should be as a child tree of order.

<order>
<staffId>1</staffId>
<status>ACTIVE</status>
<id>12</id>
<name>Order 1</name>
     <itemList>
         <item>Item 1</item>
         <item>Item 2</item>
         <item>Item 3</item>
     </itemList>
</order>

Following is my JaxB annotated class

import java.io.Serializable;
import java.util.*;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlType(propOrder = {"staffId", "status", "id", "name", "itemList"})
public class Order implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private int staffId;
    private String status;
    private String name;
    private List<String> itemList = new ArrayList<>();

    @XmlElement
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @XmlElement
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @XmlElement
    public int getStaffId() {
        return staffId;
    }
    public void setStaffId(int staffId) {
        this.staffId = staffId;
    }
    @XmlElement
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    @XmlElement
    public List<String> getItemList() {
        return itemList;
    }
    public void setItemList(List<String> itemList) {
        this.itemList = itemList;
    }
}

But the output I am getting is as per following, in which item list comes in same hierarchy as order.

<order>
    <staffId>1</staffId>
    <status>ACTIVE</status>
    <id>12</id>
    <name>Order 1</name>
    <itemList>Item 1</itemList>
    <itemList>Item 2</itemList>
    <itemList>Item 3</itemList>
</order>

Please suggest me what is missing here.

Chintan Patel
  • 729
  • 3
  • 14
  • 31

1 Answers1

1

This is a duplicate of JAXB Annotations. What you are looking for is the @XmlElementWrapperannotation used in conjunction with the @XmlElement annotation.

FWIW, Blaise Doughan's blog post on JAXB & Collection Properties has some other useful information on handling collections.

Community
  • 1
  • 1