coras09
coras09

Reputation: 343

How to Implement a POJO for an XML Element that has one single item and one list?

I'm trying to create a POJO for the following structure for deserializing an xml, which I cannot change whatsoever:

<Flat>
    <Door>
        <Type>Wood</Type>
    </Door>
    <Room>
        <Size>10</Size>
        <Unit>m2</Unit>
    </Room>
    <Room>
        <Size>22</Size>
        <Unit>m2</Unit>
    </Room>
</Flat>

The door element is singular, but how many room elements will be provided in the flat element varies. I started with the code below but did not work (and I see why, as the "Room" isn't a sub-root element that has an array of room elements):

public class FlatModel {

    @Element(name = "Door")
    private DoorModel door;

    @ElementList(name = "Room")
    private List<RoomModel> roomList;

    public FlatModel() {
    }

    public FlatModel(DoorModel door, List<RoomModel> roomList) {
    //rest is the constructors and getter/setters

But I could not find any documentation nor any answered question on how to implement such a class. Any help is appreciated.

Upvotes: 1

Views: 944

Answers (2)

coras09
coras09

Reputation: 343

Apologies. Found it on the documentation for SimpleXML. (Was searching with the wrong name).

It is explained in "Dealing with an inline list of elements" section which resides here: http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#inline

The solution for me was simply to remove the name parameter and introduce the "inline" parameter to the "ElementList" annotation as follows:

public class FlatModel {

    @Element(name = "Door")
    private DoorModel door;

    @ElementList(inline = true)
    private List<RoomModel> roomList;

    public FlatModel() {
    }

    public FlatModel(DoorModel door, List<RoomModel> roomList) {
    //rest is the constructors and getter/setters

Then, introduce the name parameter with a "Root" in the Room class itself:

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "Room")
public class Room {
//rest is the constructors and getter/setters

Upvotes: 1

Gaurav kumar Singh
Gaurav kumar Singh

Reputation: 183

If possible you need to change your xml to include a parent element for Room, e.g. Rooms and this Rooms can be deserialize to your Java POJO as list of Room.

Example:

<Flat>
    <Door>
        <Type>Wood</Type>
    </Door>
    <Rooms>
        <Room>
            <Size>10</Size>
            <Unit>m2</Unit>
        </Room>
        <Room>
            <Size>22</Size>
            <Unit>m2</Unit>
        </Room>
    </Rooms>
</Flat>

And the POJO would be

public class FlatModel {

    @Element(name = "Door")
    private DoorModel door;

    @ElementList(name = "Rooms") // changed from Room to Rooms
    private List<RoomModel> roomList;

    public FlatModel() {
    }

    public FlatModel(DoorModel door, List<RoomModel> roomList) {
    //rest is the constructors and getter/setters

Upvotes: 0

Related Questions