gpt4 book ai didi

xml - 为什么XJC生成@XmlElementRefs

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:03 25 4
gpt4 key购买 nike

我正在为来自 xsd 模式的 JAXB 注释类使用 XJC 实用程序,我有点惊讶并且不明白为什么会生成 @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&lt;Employee&gt;</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&lt;Manager&gt;</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&lt;TimeStamp&gt;</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>
* &lt;complexType name="Header">
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="CarriedOver" type="{http://www.w3.org/2001/XMLSchema}boolean"/>
* &lt;element name="CheckId" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="Closed" type="{http://www.w3.org/2001/XMLSchema}boolean"/>
* &lt;element name="Dob" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="Employees" type="{labor.pif.com}Employee" maxOccurs="unbounded" minOccurs="0"/>
* &lt;element name="GuestCountMode" type="{check.pif.com}GuestCounting"/>
* &lt;element name="Guests" type="{http://www.w3.org/2001/XMLSchema}double"/>
* &lt;element name="Managers" type="{labor.pif.com}Manager" maxOccurs="unbounded" minOccurs="0"/>
* &lt;element name="Period" type="{check.pif.com}TypeVal"/>
* &lt;element name="PrintedCheckId" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="RevCenter" type="{check.pif.com}TypeVal"/>
* &lt;element name="Room" type="{check.pif.com}TypeStrVal"/>
* &lt;element name="StoreId" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="Table" type="{check.pif.com}TypeStrVal"/>
* &lt;element name="TableType" type="{check.pif.com}TableTypeDef"/>
* &lt;element name="TerminalId" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="Times" type="{check.pif.com}TimeStamp" maxOccurs="unbounded" minOccurs="0"/>
* &lt;element name="Training" type="{http://www.w3.org/2001/XMLSchema}boolean"/>
* &lt;element name="Managers" type="{labor.pif.com}Manager" maxOccurs="unbounded" minOccurs="0"/>
* &lt;element name="Times" type="{check.pif.com}TimeStamp" maxOccurs="unbounded" minOccurs="0"/>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/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 ...
}

有什么我想念的吗?

最佳答案

这是因为您有一个名为 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&lt;Manager&gt;</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&lt;Manager&gt;</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;

关于xml - 为什么XJC生成@XmlElementRefs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20123171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com