我正在使用XJC实用程序来处理来自xsd架构的JAXB注释类,我有点意外,并且不明白为什么会生成@XmlElementRefs。所有这些都发生在以下xsd片段中(这里省略了名称空间)
<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>
xjc生成的java类如下:
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;
}
}
对我而言,似乎奇怪的是所有内容都包含在JaxbElement中 - 很难处理它。我希望有类似的东西:
public class Header {
private Boolean CarriedOver;
private String CheckId;
private Boolean Closed;
private String Dob;
private List<Employee> Employees;
...
propper getters and setters ...
}
我有什么遗失的吗?
答案 0 :(得分:5)
这是因为你有一个名为Managers
的元素在你的序列中出现两次。为了能够往返文档,JAXB需要将所有数据存储在有序的List
中。
以下是您的用例的简化版本:
<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>
生成的Header
类将具有映射为@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;
以下是复杂类型的修改版本,其中不是两个使用名称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>
将为每个元素生成单独的字段/属性。
@XmlElement(name = "Managers")
protected List<Manager> managers;
@XmlElement(name = "FOO", required = true)
protected String foo;