Reputation: 45
this is my definition that don't retrieve the list of elements in the wrapped elements:
@XmlElementWrapper(name = "channels")
@XmlElement(name = "channel")
private ArrayList<Channel> expChannels;
public ArrayList<Channel> getChannels() {
return this.expChannels;
}
public void setChannels(ArrayList<Channel> listOfChannels) {
this.expChannels = listOfChannels;
}
And this is the declaration of Channel model object:
@XmlRootElement
public class Channel {
//Channel() {}
@XmlAttribute(name = "id")
private Integer channelId;
getters/setters
When I get data from xml, any channel is retrieved.
I've achived to work with an intermediate class Channels, changing the code to:
private Channels expChannels;
@XmlElement(name = "channels")
public Channels getChannels() {
return this.expChannels;
}
public void setChannels(Channels listOfChannels) {
this.expChannels = listOfChannels;
}
and defining Channels:
@XmlRootElement(name="channels")
public class Channels {
private List<Channel> expChannels = new ArrayList<Channel>();
@XmlElement(name = "channel")
getters/setters
This is the structure of XML file:
<experiment xmlns="experiment">
<name>Test Experiment</name>
<file>ExperimentTemplate.xml</file>
<channels> <!-- List of channel's -->
<channel>
<id>0</id>
<name>Channel 1</name>
<description>Channel 1 description</description>
</channel>
<channel>
<id>1</id>
<name>Channel 2</name>
<description>Channel 1 description</description>
</channel>
</channels>
</experiment>
Can I avoid the use of Channels class?
Upvotes: 1
Views: 2532
Reputation: 997
I had the same problem. In your case you need to add the namespace to XmlWrapper
@XmlElementWrapper(name = "channels", namespace = "experiment")
@XmlElement(name = "channel", namespace = "experiment")
private ArrayList<Channel> expChannels;
I don't understand why a lone @XmlElment annotation does not need the namespace but @XmlElementWrapper does need it, but that's how it is.
It also works with a package level annotation. That way you need to specifiy the namespace only once and all @XmlElementWrapper annotations in the whole package work:
@XmlSchema(namespace = "experiment", elementFormDefault = XmlNsForm.QUALIFIED, xmlns = { @XmlNs(prefix = "", namespaceURI = "experiment") })
Upvotes: 2
Reputation: 149047
@XmlElementWrapper
definitely works. For unmarshalling issues it is best to populate your object model and marshal it and compare the result to the XML you are unmarshalling.
Upvotes: 0