35.2. 属性
概述
Apache CXF 支持在 复杂Type
元素范围内使用 属性元素和属性Group
元素。在为 XML 文档属性声明定义结构时,提供了添加在标签中指定的信息而不是标签包含的值的方法。例如,在 XML Schema 中描述 XML 元素 <value
currency
="euro">410<\value> 时,使用 例 35.5 “XML Schema 重新定义和属性” 所示 的属性
元素描述。
attributeGroup
元素允许您定义一组可重复使用的属性,这些属性可以被 schema 定义的所有复杂类型引用。例如,如果您定义了一系列使用属性类别和 pubDate
的元素,您可以使用这些属性来定义属性组,并在所有使用该属性的元素中引用它们。这显示在 例 35.7 “属性组定义” 中。
在描述开发应用逻辑中使用的数据类型时,使用
属性的属性设置为 可选
或 必需
属性被视为结构的元素。对于复杂类型描述中包含的每个属性声明,会在类中生成一个元素,以及适当的 getter 和 setter 方法。
在 XML Schema 中定义属性
XML Schema 属性
元素具有一个必需属性名称,用于标识该属性。它还有四个可选属性,如 表 35.2 “用于定义 XML Schema 中的属性” 所述。
属性 | 描述 |
---|---|
|
指定是否需要该属性。有效值 |
| 指定属性可取的值类型。如果未使用属性的模式类型,则必须在命令行中定义。 |
|
指定要用于属性的默认值。仅在 |
|
指定用于属性的固定值。仅在 |
例 35.5 “XML Schema 重新定义和属性” 显示属性元素,定义属性货币,其值是一个字符串。
例 35.5. XML Schema 重新定义和属性
<element name="value"> <complexType> <xsd:simpleContent> <xsd:extension base="xsd:integer"> <xsd:attribute name="currency" type="xsd:string" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element>
如果属性元素省略了 type
属性
,则必须描述数据的格式。例 35.6 “带有在线数据描述的属性” 显示属性、类别
的属性
元素,其可取值 自动
、非
虚构
或虚假。
例 35.6. 带有在线数据描述的属性
<attribute name="category" use="required"> <simpleType> <restriction base="xsd:string"> <enumeration value="autobiography"/> <enumeration value="non-fiction"/> <enumeration value="fiction"/> </restriction> </simpleType> </attribute>
在 XML Schema 中使用属性组
在复杂类型定义中使用属性组分为两个步骤:
定义属性组。
属性组通过使用
attribute
GroupattributeGroup
需要一个name
属性,用于定义用于引用属性组的字符串。属性
元素定义属性组的成员,并以 “在 XML Schema 中定义属性”一节 的形式指定。例 35.7 “属性组定义” 显示属性组catalogIndecies
的描述。属性组有两个成员:类别
,是可选的,以及pubDate
,这是所需的。例 35.7. 属性组定义
<attributeGroup name="catalogIndices"> <attribute name="category" type="catagoryType" /> <attribute name="pubDate" type="dateTime" use="required" /> </attributeGroup>
在复杂类型的定义中使用 属性组。
您可以使用带有
ref
属性的attributeGroup
元素,在复杂类型定义中使用属性组。ref
属性的值是授予您想要用作类型定义一部分的属性组群的名称。例如,如果要使用复杂类型dvdType
中的属性组catalogIndecies
,您可以使用 <attributeGroup ref="catalogIndecies" />。例 35.8 “带有属性组的复杂类型”例 35.8. 带有属性组的复杂类型
<complexType name="dvdType"> <sequence> <element name="title" type="xsd:string" /> <element name="director" type="xsd:string" /> <element name="numCopies" type="xsd:int" /> </sequence> <attributeGroup ref="catalogIndices" /> </complexType>
将属性映射到 Java
属性与成员元素映射到 Java 的方式完全映射到 Java。所需的属性和可选属性映射到生成的 Java 类的成员变量。成员变量通过 @XmlAttribute
注释进行解码。如果需要 属性,则 @XmlAttribute
注释 的必需
属性设为 true
。
例 35.9 “techDoc Description” 中定义的复杂类型映射到 例 35.10 “techDoc Java Class” 中显示的 Java 类。
例 35.9. techDoc Description
<complexType name="techDoc"> <all> <element name="product" type="xsd:string" /> <element name="version" type="xsd:short" /> </all> <attribute name="usefullness" type="xsd:float" use="optional" default="0.01" /> </complexType>
例 35.10. techDoc Java Class
@XmlType(name = "techDoc", propOrder = { }) public class TechDoc { @XmlElement(required = true) protected String product; protected short version; @XmlAttribute protected Float usefullness; public String getProduct() { return product; } public void setProduct(String value) { this.product = value; } public short getVersion() { return version; } public void setVersion(short value) { this.version = value; } public float getUsefullness() { if (usefullness == null) { return 0.01F; } else { return usefullness; } } public void setUsefullness(Float value) { this.usefullness = value; } }
如 例 35.10 “techDoc Java Class” 所示,default
属性和 fixed
属性指示代码生成器将代码添加到为属性生成的 getter 方法中。这个附加代码可确保如果没有设置值,则会返回指定的值。
fixed
属性的处理方式与 default
属性相同。如果您希望 固定
属性被视为 Java 持续,您可以使用 第 38.5 节 “自定义修复的值属性映射” 中描述的自定义。
将属性组映射到 Java
属性组映射到 Java,就如类型定义中明确使用组的成员一样。如果属性组有三个成员,且在复杂的类型中使用,该类型的生成的类将包含 member 变量,以及 getter 和 setter 方法,用于属性组的每个成员。例如,例 35.8 “带有属性组的复杂类型” 中定义的复杂类型,Apache CXF 生成包含成员变量类别和 pubDate
的类,以支持属性组成员,如 例 35.11 “dvdType Java Class” 所示。
例 35.11. dvdType Java Class
@XmlType(name = "dvdType", propOrder = {
"title",
"director",
"numCopies"
})
public class DvdType {
@XmlElement(required = true)
protected String title;
@XmlElement(required = true)
protected String director;
protected int numCopies;
@XmlAttribute protected CatagoryType category; @XmlAttribute(required = true) @XmlSchemaType(name = "dateTime") protected XMLGregorianCalendar pubDate;
public String getTitle() {
return title;
}
public void setTitle(String value) {
this.title = value;
}
public String getDirector() {
return director;
}
public void setDirector(String value) {
this.director = value;
}
public int getNumCopies() {
return numCopies;
}
public void setNumCopies(int value) {
this.numCopies = value;
}
public CatagoryType getCatagory() {
return catagory;
}
public void setCatagory(CatagoryType value) {
this.catagory = value;
}
public XMLGregorianCalendar getPubDate() {
return pubDate;
}
public void setPubDate(XMLGregorianCalendar value) {
this.pubDate = value;
}
}