- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在为来自 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<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 ...
}
有什么我想念的吗?
最佳答案
这是因为您有一个名为 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;
关于xml - 为什么XJC生成@XmlElementRefs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20123171/
有人可以给我解释一下吗 @XmlElementRefs 和 @XmlElementRef 注解在 Java 中的含义及其用途.. EDIT: @skaffman 好吧,假设我有一个这样的 Collec
当我尝试在 SpringToolSuite 中进行调试时。它有时会引发异常 Invalid XmlElementRef on property expression on class org.open
我正在为来自 xsd 模式的 JAXB 注释类使用 XJC 实用程序,我有点惊讶并且不明白为什么会生成 @XmlElementRefs。一切都发生在以下 xsd 片段中(此处省略命名空间)
我使用以下带注释的类通过 MOXy 2.4.0 进行解码。解码到该对象中效果很好 @XmlElementRefs({ @XmlElementRef(name = "a", namesp
我有以下类(class) @XmlRootElement(name = "entity") public class Entity { @XmlElementRef protected
您好,有人可以解释一下如何解决以下问题吗?我有这样的类结构.. public class RequestWrapper implements Seriallizable { private
我有一些类(class),例如... @XmlRootElement(name="Predicate") @XmlAccessorType(XmlAccessType.FIELD) public ab
我对 Web 服务请求和响应 XML 使用相同的 @XmlElementRef 元素,例如: 请求: @XmlElementRef(name = "elementId", namespace = "b
我没有将名称作为 @XmlElementRef 的标记名称。 父elemnet中的元素声明: @XmlElementRef(name = "Agents", type = JAXBElement.c
是否可以在使用@XmlElementRef 注释的引用对象上使用@XmlValue 注释?目标是生成以下 XML 输出: Blah 给定以下示例 JAXBContex
这个问题已经以不同的方式被问过多次,但我在这里从不同的角度提出疑问。那么让我们来看下面的例子: 对于 XSD 中的上一行,JAXB 生成以下代码: @XmlElementRef(name = "Ri
我在使用 jdk 1.6 时遇到以下错误,但它在 1.7 中工作 cannot find symbol method required() javax.xml.bind.annotation.XmlE
我有 4 个类(class)。 Person 类,以及抽象的 ContactInformation,Phone 和 Address 类对其进行扩展。 @XmlRootElement @XmlAcces
我正在尝试使用 maven-jaxb2-plugin 编译位于 http://www.aptatcip.com/APTA-TCIP-S-01%204.0_files/Schema%20Set.zip
我是一名优秀的程序员,十分优秀!