Reputation: 3405
I am using and XJC utility for JAXB annotated classes from xsd schema and I am a bit surprised and don't understand why @XmlElementRefs gets generated. All happens in following xsd snippet (namespaces omitted here)
<xs:complexType name="Header">
<xs:sequence>
<xs:element name="CarriedOver" type="xs:boolean" minOccurs="1" maxOccurs="1"/>
<xs:element name="CheckId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="Closed" type="xs:boolean" minOccurs="1" maxOccurs="1"/>
<xs:element name="Dob" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="Employees" type="lb:Employee" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>List<Employee></xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="GuestCountMode" type="ch:GuestCounting" minOccurs="1" maxOccurs="1"/>
<xs:element name="Guests" type="xs:double" minOccurs="1" maxOccurs="1"/>
<xs:element name="Managers" type="lb:Manager" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>List<Manager></xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="Period" type="ch:TypeVal" minOccurs="1" maxOccurs="1"/>
<xs:element name="PrintedCheckId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="RevCenter" type="ch:TypeVal" minOccurs="1" maxOccurs="1"/>
<xs:element name="Room" type="ch:TypeStrVal" minOccurs="1" maxOccurs="1"/>
<xs:element name="StoreId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="Table" type="ch:TypeStrVal" minOccurs="1" maxOccurs="1"/>
<xs:element name="TableType" type="ch:TableTypeDef" minOccurs="1" maxOccurs="1"/>
<xs:element name="TerminalId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="Times" type="ch:TimeStamp" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>List<TimeStamp></xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="Training" type="xs:boolean" minOccurs="1" maxOccurs="1"/>
<xs:element name="Managers" type="lb:Manager" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="Times" type="ch:TimeStamp" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
Generated java class by xjc is following:
package com.pif.check;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlType;
import com.pif.labor.Employee;
import com.pif.labor.Manager;
/**
* <p>Java class for Header complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="Header">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="CarriedOver" type="{http://www.w3.org/2001/XMLSchema}boolean"/>
* <element name="CheckId" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Closed" type="{http://www.w3.org/2001/XMLSchema}boolean"/>
* <element name="Dob" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Employees" type="{labor.pif.com}Employee" maxOccurs="unbounded" minOccurs="0"/>
* <element name="GuestCountMode" type="{check.pif.com}GuestCounting"/>
* <element name="Guests" type="{http://www.w3.org/2001/XMLSchema}double"/>
* <element name="Managers" type="{labor.pif.com}Manager" maxOccurs="unbounded" minOccurs="0"/>
* <element name="Period" type="{check.pif.com}TypeVal"/>
* <element name="PrintedCheckId" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="RevCenter" type="{check.pif.com}TypeVal"/>
* <element name="Room" type="{check.pif.com}TypeStrVal"/>
* <element name="StoreId" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Table" type="{check.pif.com}TypeStrVal"/>
* <element name="TableType" type="{check.pif.com}TableTypeDef"/>
* <element name="TerminalId" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Times" type="{check.pif.com}TimeStamp" maxOccurs="unbounded" minOccurs="0"/>
* <element name="Training" type="{http://www.w3.org/2001/XMLSchema}boolean"/>
* <element name="Managers" type="{labor.pif.com}Manager" maxOccurs="unbounded" minOccurs="0"/>
* <element name="Times" type="{check.pif.com}TimeStamp" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Header", propOrder = {
"content"
})
public class Header {
@XmlElementRefs({
@XmlElementRef(name = "Guests", type = JAXBElement.class, required = false),
@XmlElementRef(name = "TableType", type = JAXBElement.class, required = false),
@XmlElementRef(name = "PrintedCheckId", type = JAXBElement.class, required = false),
@XmlElementRef(name = "RevCenter", type = JAXBElement.class, required = false),
@XmlElementRef(name = "Dob", type = JAXBElement.class, required = false),
@XmlElementRef(name = "Employees", type = JAXBElement.class, required = false),
@XmlElementRef(name = "Room", type = JAXBElement.class, required = false),
@XmlElementRef(name = "Table", type = JAXBElement.class, required = false),
@XmlElementRef(name = "Closed", type = JAXBElement.class, required = false),
@XmlElementRef(name = "GuestCountMode", type = JAXBElement.class, required = false),
@XmlElementRef(name = "Times", type = JAXBElement.class, required = false),
@XmlElementRef(name = "CheckId", type = JAXBElement.class, required = false),
@XmlElementRef(name = "Period", type = JAXBElement.class, required = false),
@XmlElementRef(name = "Managers", type = JAXBElement.class, required = false),
@XmlElementRef(name = "StoreId", type = JAXBElement.class, required = false),
@XmlElementRef(name = "TerminalId", type = JAXBElement.class, required = false),
@XmlElementRef(name = "Training", type = JAXBElement.class, required = false),
@XmlElementRef(name = "CarriedOver", type = JAXBElement.class, required = false)
})
protected List<JAXBElement<?>> content;
/**
* Gets the rest of the content model.
*
* <p>
* You are getting this "catch-all" property because of the following reason:
* The field name "Managers" is used by two different parts of a schema. See:
* line 160 of file:/C:/Users/JS185350/Documents/WorkSpace/WorkspaceEA/schemas/pif/check.xsd
* line 141 of file:/C:/Users/JS185350/Documents/WorkSpace/WorkspaceEA/schemas/pif/check.xsd
* <p>
* To get rid of this property, apply a property customization to one
* of both of the following declarations to change their names:
* Gets the value of the content property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the content property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getContent().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link JAXBElement }{@code <}{@link Double }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link TableTypeDef }{@code >}
* {@link JAXBElement }{@code <}{@link TypeVal }{@code >}
* {@link JAXBElement }{@code <}{@link Employee }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link TypeStrVal }{@code >}
* {@link JAXBElement }{@code <}{@link Boolean }{@code >}
* {@link JAXBElement }{@code <}{@link TypeStrVal }{@code >}
* {@link JAXBElement }{@code <}{@link GuestCounting }{@code >}
* {@link JAXBElement }{@code <}{@link TypeVal }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link TimeStamp }{@code >}
* {@link JAXBElement }{@code <}{@link Manager }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link String }{@code >}
* {@link JAXBElement }{@code <}{@link Boolean }{@code >}
* {@link JAXBElement }{@code <}{@link Boolean }{@code >}
*
*
*/
public List<JAXBElement<?>> getContent() {
if (content == null) {
content = new ArrayList<JAXBElement<?>>();
}
return this.content;
}
}
To me it seems strange that all is wrapped in JaxbElement - it is hard to work with that. I would expect something like:
public class Header {
private Boolean CarriedOver;
private String CheckId;
private Boolean Closed;
private String Dob;
private List<Employee> Employees;
...
propper getters and setters ...
}
Is there something I am missing?
Upvotes: 5
Views: 3375
Reputation: 149047
It is because you have an element called Managers
that occurs twice in your sequence. In order to be able to round trip the document JAXB needs to then store all the data in an ordered List
.
Below is a simplified version of your use case:
<xs:complexType name="Header">
<xs:sequence>
<xs:element name="Managers" type="lb:Manager" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>List<Manager></xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="FOO" type="xs:string"/>
<xs:element name="Managers" type="lb:Manager" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
The generated Header
class would have a property mapped with @XmlElementRefs
:
@XmlElementRefs({
@XmlElementRef(name = "Managers", namespace = "http://www.example.org/foo", type = JAXBElement.class),
@XmlElementRef(name = "FOO", namespace = "http://www.example.org/foo", type = JAXBElement.class)
})
protected List<JAXBElement<?>> content;
Below is a modified version of your complex type where their aren't two elements defined with the name Managers
.
<xs:complexType name="Header">
<xs:sequence>
<xs:element name="Managers" type="lb:Manager" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>List<Manager></xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="FOO" type="xs:string"/>
</xs:sequence>
</xs:complexType>
A separate field/property will be generated for each element.
@XmlElement(name = "Managers")
protected List<Manager> managers;
@XmlElement(name = "FOO", required = true)
protected String foo;
Upvotes: 8