Reputation: 772
I've got this xml code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<beer>some beer</beer>
<beer>some another beer</beer>
<food>some food</food>
<food>some another food</food>
</root>`enter code here`
To unmarshalling this xml with JAXB I'm using this source:
@XmlElement(name="beer")
public void setKey(Set<String> key)
{
this.key = key;
}
When I unmarshalling xml for Set I've receive the result: "some beer" and "some another beer", because the anotation and the tag name. So how can I parse all childs from the "root" tag. i.e. result in Set have to be: some beer, some another beer, some food, some another food.
Thanks previously for the time spend for my issue.
Upvotes: 0
Views: 87
Reputation: 489
Do you really need JAXB for this? If so, maybe something like this will help:
import java.util.ArrayList;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"beer",
"food"
})
@XmlRootElement(name = "root")
public class Root {
@XmlElement(required = true)
protected List<Beer> beer;
@XmlElement(required = true)
protected List<Food> food;
public List<Beer> getBeer() {
if (beer == null) {
beer = new ArrayList<Beer>();
}
return this.beer;
}
public List<Food> getFood() {
if (food == null) {
food = new ArrayList<Food>();
}
return this.food;
}
}
Also you need to create Food and Beer classes with getters and setters. For lists there is no need to create setters, because you can use add() after getter. But I recommend to use DOM for this task.
Upvotes: 1