JAXB: generating item code with maxOccurs property set in xsd file doesn't get set method in java class
I have an xsd file named Person
with some items. Some elements have both properties minOccurs
and maxOccurs
. The two lines in the xsd file might look like this.
<xsd:element name="NameOfElement" minOccurs="0" maxOccurs="unbounded">
<xsd:element name="NameOfAnotherElement" minOccurs="0">
In NetBeans, I want to generate java classes of this xsd file using JAXB. All items that have a property minOccurs
get both set and getter in the generated java file Person
, but items that have both properties minOccurs
and maxOccurs
set in the xsd file become List. So the above xsd lines become after generation:
@XmlElement(name = NameOfElement)
protected List<Person.NameOfElement> nameOfElement;
@XmlElement(name = NameOfAnotherElement)
protected Person.NameOfAnotherElement nameOfAnotherElement;
The weird thing is that the variable nameOfAnotherElement
gets both set and get method in Person
java class and nameOfElement
gets get method.
Why don't elements that become List<>
in Java code get a set method (those elements that have both properties minOccurs
and maxOccurs
set in xsd)?
So my problem is that I cannot set the NameOfElement on the Person object because it skips the set method but contains a get method! Why is this so?
source to share
If you have maxOccurs set to! = 1, it can contain multiple instances of that element, so it becomes a list.
You have to use the get method and then add items to this list. Something like that:
List<Person.NameOfElement> myList = doc.getNameOfElement();
myList.add(obj);
Edit: if you already have a list that you want to use, you can do the following:
doc.getNameOfElement().addAll(myList);
source to share