users@jaxb.java.net

JAXB - wrong getter method?

From: Yoann Moranville <yoann.moranville_at_gmail.com>
Date: Mon, 29 Apr 2013 16:04:47 +0200

Hello all,

I have a problem using JAXB (com.sun.xml.bind - jaxb-impl - 2.2.6), so I
must either be doing something wrong or there is a bug somewhere. I hope
I can get some help from you.
I have an XML schema which I used to create the object classes, I will
add the corresponding code in the mail.
The problem seems to be that I am loosing data on the way.


XSD:

<xs:group name="m.dates">
         <xs:choice>
             <xs:element name="date" type="date"/>
             <xs:element name="dateRange" type="dateRange"/>
             <xs:element name="dateSet" type="dateSet"/>
         </xs:choice>
     </xs:group>
     <xs:complexType name="dateSet">
             <xs:sequence>
                 <xs:choice>
                     <xs:element name="date" type="date"/>
                     <xs:element name="dateRange" type="dateRange"/>
                 </xs:choice>
                 <xs:choice maxOccurs="unbounded">
                     <xs:element name="date" type="date"/>
                     <xs:element name="dateRange" type="dateRange"/>
                 </xs:choice>
             </xs:sequence>
     </xs:complexType>



Object:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
     "date",
     "dateRange",
     "dateOrDateRange"
})
@XmlRootElement(name = "dateSet")
public class DateSet {

     protected Date date;
     protected DateRange dateRange;
     @XmlElements({
         @XmlElement(name = "date", type = Date.class),
         @XmlElement(name = "dateRange", type = DateRange.class)
     })
     protected List<Object> dateOrDateRange;

     public Date getDate() {
         return date;
     }

     public void setDate(Date value) {
         this.date = value;
     }

     public DateRange getDateRange() {
         return dateRange;
     }

     public void setDateRange(DateRange value) {
         this.dateRange = value;
     }

     public List<Object> getDateOrDateRange() {
         if (dateOrDateRange == null) {
             dateOrDateRange = new ArrayList<Object>();
         }
         return this.dateOrDateRange;
     }
}


Unfortunately, when using opening an XML file containing this part:
                         <dateSet>
                             <date>2000</date>
                             <date>2001</date>
                             <dateRange>
<fromDate>2002</fromDate>
                                 <toDate>2011</toDate>
                             </dateRange>
                         </dateSet>


Then I loose the first date element. So all the rest gets send to
DateSet/Date and DateSet/DateRange but not the first one. I would have
expected to retrieve the data via the List<Object> getDateOrDateRange()
method since I have more than one "date" element but it does not seem to
be the case.

Anyone has any idea of what I am doing wrong? If you need more code or
explanation, please let me know.

Cheers,
Yoann