yatinbc yatinbc - 24 days ago 6
Java Question

How to get following XML structure using JaxB

I have requirement where I need to generate XML having following structure.
Data I am populating from database but this is going to be format for the target XML.
I have created few classes and tried marshalling those bean but I am not able to generate following structure.
Please help.

<Component id ="668020">--root element
<xyz>xyz</xyz>
<pqr>pqr</pqr>
<Profile>
<ABc>abc</ABc>
<Bcd>bcd</Bcd>
</Profile>
<TwentySeven>
<Item>
<one>1</one>
<two>2</two>
</Item>
<Item>
<one>1</one>
<two>2</two>
</Item>
<Item>
<one>1</one>
<two>2</two>
</Item>
</TwentySeven>
<Hundred>
<Hundred_One>
<Item>
<one>1</one>
<two>2</two>
</Item>
<Item>
<one>1</one>
<two>2</two>
</Item>
<Item>
<one>1</one>
<two>2</two>
</Item>
</Hundred_one>
</Hundred>
</Component>

Answer

Assuming your XML structure to be precise, here are the sample Java class structures you need to follow inorder to work along with JaxB: This will be your Root element class.

@XmlRootElement
public class Component {
    private int id;

    private Profile profile;

    private TwentySeven twentySeven;

    private Hundred hundred;

    public Hundred getHundred() {
        return hundred;
    }

    @XmlElement
    public void setHundred(Hundred hundred) {
        this.hundred = hundred;
    }

    public int getId() {
        return id;
    }

    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }

    /*
     * Same way add @XMLElement annotation to
     * setter methods of profile and twentySeven.
     */

}

Other sample classes are as shown below:

public class Hundred {
    private List<Item> items;

    public List<Item> getItems() {
        return items;
    }

    @XmlElement
    public void setItems(List<Item> items) {
        this.items = items;
    }
}

public class TwentySeven {
    private List<Item> items;

    public List<Item> getItems() {
        return items;
    }

    @XmlElement
    public void setItems(List<Item> items) {
        this.items = items;
    }
}

public class Item {
    //private <Object of type One>
    //private <Object of type Two>

    /*
     * Write getter setter methods and add annotations
     * accordingly as shown in other sample methods.
     */
}

public class Profile {
    private Abc item;

    public Abc getItem() {
        return item;
    }

    @XmlElement
    public void setItem(Abc item) {
        this.item = item;
    }

    //Bcd also goes in same way
}

public class Abc {
    private String param1;

    private String param2;

    public String getParam1() {
        return param1;
    }

    @XmlElement
    public void setParam1(String param1) {
        this.param1 = param1;
    }

    public String getParam2() {
        return param2;
    }

    @XmlElement
    public void setParam2(String param2) {
        this.param2 = param2;
    }
}

I hope this should help you get going.

Note: The classes and methods written in this example are just samples. You need to modify them according to your needs.