How do you create a POJO containing a list that the container has attributes?
Typically when creating a POJO of a list you do it the following way:
To represent the following XML structure:
<folder>
<messages>
<message>
<subject>XXXX</subject>
...
</message>
<message>
<subject>XXXX</subject>
...
</message>
</messages>
</folder>
@XmlRootElement(name = "folder")
public class Folder {
@XmlElement
private List<Message> messages;
...
}
@XmlRootElement(name = "message")
public class Message {
@XmlElement
private String subject;
...
}
But how do you represent a POJO when there are attributes at the messages tag? i.e.
<folder>
<messages total="45" start="3">
<message>
<subject>XXXX</subject>
...
</message>
<message>
<subject>XXXX</subject>
...
</message>
</messages>
</folder>
Do you create a POJO specifically for messages and then map a List of Message with an annotation of @XmlValue
or something along those lines?
Thanks for your help guys.
The following approach could be used with any JAXB (JSR-222) implementation.
Messages
Using just the standard JAXB (JSR-222) APIs you will need to introduce a Messages
class to your model.
import java.util.List;
import javax.xml.bind.annotation.*;
public class Messages {
@XmlElement(name="message")
private List<Message> messages;
@XmlAttribute
private int start;
@XmlAttribute
public int getTotal() {
if(null == messages) {
return 0;
} else {
return messages.size();
}
}
}
Folder
Then you will need to modify your Folder
class to reference the new Messages
class.
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Folder {
private Messages messages;
}
Message
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Message {
private String subject;
}