Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
The following section describes several examples that build on the concepts demonstrated in the Basic examples.
The goal of this section is to illustrate how to customize JAXB bindings using custom binding declarations, made in one of the following two ways:
Unlike the examples in Basic JAXB Examples, which focus on the Java code in the respective Main.java class files, the examples here focus on customizations made to the XML schema before generating the schema-derived Java binding classes.
This section introduces customizations that can be made to JAXB bindings and validation methods. For more information, see JAXB Specification.
In most cases, the default bindings generated by the JAXB binding compiler are sufficient. There are cases, however, in which you might want to modify the default bindings. Some of these include:
This section explains some core JAXB customization concepts:
Customizations to the default JAXB bindings are made in the form of binding declarations passed to the JAXB binding compiler. These binding declarations can be made in either of two ways:
For some, using inline customizations is easier because you can see your customizations in the context of the schema to which they apply. Conversely, using an external binding customization file enables you to customize JAXB bindings without modifying the source schema, and enables you to easily apply customizations to several schema files at once.
Each of these types of customization is described in more detail in the following sections:
Customizations to JAXB bindings made by means of inline binding declarations in an XML schema file take the form of <xsd:appinfo> elements embedded in schema <xsd:annotation> elements (xsd: is the XML schema namespace prefix, as defined in W3C XML Schema Part 1: Structures). The general form for inline customizations is shown in the following example:
<xs:annotation> <xs:appinfo> <!-- ... binding declarations . ... --> </xs:appinfo> </xs:annotation>
Customizations are applied at the location at which they are declared in the schema. For example, a declaration at the level of a particular element applies to that element only. Note that the XML schema namespace prefix must be used with the <annotation> and <appinfo> declaration tags. In the previous example, xs: is used as the namespace prefix, so the declarations are tagged <xs:annotation> and <xs:appinfo>.
Customizations to JAXB bindings made by using an external file containing binding declarations, take the general form shown in the following example:
<jxb:bindings schemaLocation = "xs:anyURI"> <jxb:bindings node = "xs:string">* <!-- binding declaration --> <jxb:bindings> </jxb:bindings>
For example, the first schemaLocation/node declaration in a JAXB binding declarations file specifies the schema name and the root schema node:
<jxb:bindings schemaLocation="po.xsd" node="/xs:schema"> </jxb:bindings>
A subsequent schemaLocation/node declaration, such as a simpleType element named ZipCodeType in the previous schema example, takes the following form:
<jxb:bindings node="//xs:simpleType [@name=’ZipCodeType’]">
Binding customization files must be in ASCII text. The name or extension does not matter; although a typical extension, used in this chapter, is .xjb.
Customization files containing binding declarations are passed to the JAXB Binding compiler, xjc, using the following syntax:
xjc -b file schema
where file is the name of binding customization file, and schema is the name of the schemas you want to pass to the binding compiler.
You can have a single binding file that contains customizations for multiple schemas, or you can seperate the customizations into multiple bindings files; for example:
xjc schema1.xsd schema2.xsd schema3.xsd \ -b bindings123.xjb xjc schema1.xsd schema2.xsd schema3.xsd \ -b bindings1.xjb \ -b bindings2.xjb \ -b bindings3.xjb
Note that the ordering of schema files and binding files on the command-line does not matter; although each binding customization file must be preceded by its own -b switch on the command line.
For more information about the xjc compiler options in general, see JAXB Compiler Options.
There are several rules that apply to binding declarations made in an external binding customization file that do not apply to similar declarations made inline in a source schema:
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
Similarly, individual nodes within the schema to which customizations are to be applied must be specified using XPath notation; for example:
<jxb:bindings node="//xs:complexType [@name=’USAddress’]">
In such cases, the customization is applied to the node by the binding compiler as if the declaration was embedded inline in the node’s <xs:appinfo> element.
To summarize these rules, the external binding element <jxb:bindings> is only recognized for processing by a JAXB binding compiler in three cases:
Default JAXB bindings can be customized or overridden at four different levels, or scopes.
The following figure illustrates the inheritance and precedence of customization declarations. Specifically, declarations towards the top of the pyramid inherit and supersede declarations below them.
Component declarations inherit from and supersede Definition declarations; Definition declarations inherit and supersede Schema declarations; and Schema declarations inherit and supersede Global declarations.
Figure: Customization Scope Inheritance and Precedence
The syntax for the four types of JAXB binding declarations, the syntax for the XML-to-Java data type binding declarations, and the customization namespace prefix are described in the following section.
Global scope customizations are declared with <globalBindings>. The syntax for global scope customizations is as follows:
<globalBindings> [ collectionType = "collectionType" ] [ fixedAttributeAsConstantProperty = "true" | "false" | "1" | "0" ] [ generateIsSetMethod = "true" | "false" | "1" | "0" ] [ enableFailFastCheck = "true" | "false" | "1" | "0" ] [ choiceContentProperty = "true" | "false" | "1" | "0" ] [ underscoreBinding = "asWordSeparator" | "asCharInWord" ] [ typesafeEnumBase = "typesafeEnumBase" ] [ typesafeEnumMemberName = "generateName" | "generateError" ] [ enableJavaNamingConventions = "true" | "false" | "1" | "0" ] [ bindingStyle = "elementBinding" | "modelGroupBinding" ] [ <javaType> ... </javaType> ]* </globalBindings>
<globalBindings> declarations are only valid in the annotation element of the top-level schema element. There can only be a single instance of a <globalBindings> declaration in any given schema or binding declarations file. If one source schema includes or imports a second source schema, the <globalBindings> declaration must be declared in the first source schema.
Schema scope customizations are declared with <schemaBindings>. The syntax for schema scope customizations is:
<schemaBindings> [ <package> package </package> ] [ <nameXmlTransform> ... </nameXmlTransform> ]* </schemaBindings> <package [ name = "packageName" ] [ <javadoc> ... </javadoc> ] </package> <nameXmlTransform> [ <typeName [ suffix="suffix" ] [ prefix="prefix" ] /> ] [ <elementName [ suffix="suffix" ] [ prefix="prefix" ] /> ] [ <modelGroupName [ suffix="suffix" ] [ prefix="prefix" ] /> ] [ <anonymousTypeName [ suffix="suffix" ] [ prefix="prefix" ] /> ] </nameXmlTransform>
As shown above, <schemaBinding> declarations include two subcomponents:
The <class> binding declaration enables you to customize the binding of a schema element to a Java content interface or a Java Element interface. <class> declarations can be used to customize:
The syntax for <class> customizations is:
<class [ name = "className"] [ implClass= "implClass" ] > [ <javadoc> ... </javadoc> ] </class>
The <property> binding declaration enables you to customize the binding of an XML schema element to its Java representation as a property. The scope of customization can either be at the definition level or component level depending upon where the <property> binding declaration is specified.
The syntax for <property> customizations is:
<property [ name = "propertyName"] [ collectionType = "propertyCollectionType" ] [ fixedAttributeAsConstantProperty = "true" | "false" | "1" | "0" ] [ generateIsSetMethod = "true" | "false" | "1" | "0" ] [ enableFailFastCheck ="true" | "false" | "1" | "0" ] [ <baseType> ... </baseType> ] [ <javadoc> ... </javadoc> ] </property> <baseType> <javaType> ... </javaType> </baseType>
The <javaType> declaration provides a way to customize the translation of XML data types to and from Java data types. XML provides more data types than Java, so the <javaType> declaration enables you to specify custom data type bindings when the default JAXB binding cannot sufficiently represent your schema.
The target Java data type can be a Java built-in data type or an application-specific Java data type. If an application-specific Java data type is used as the target, your implementation must also provide parse and print methods for unmarshalling and marshalling data. To this end, the JAXB specification supports a parseMethod and printMethod:
If you prefer to define your own data type conversions, JAXB defines a static class, DatatypeConverter, to assist in the parsing and printing of valid lexical representations of the XML Schema built-in data types.
The syntax for the <javaType> customization is:
<javaType name= "javaType" [ xmlType= "xmlType" ] [ hasNsContext = "true" | "false" ] [ parseMethod= "parseMethod" ] [ printMethod= "printMethod" ]>
The <javaType> declaration can be used in:
See MyDatatypeConverter Class for an example of how <javaType> declarations and the DatatypeConverterInterface interface are implemented in a custom data type converter class.
The typesafe enumeration declarations provide a localized way to map XML simpleType elements to Java typesafe enum classes. There are two types of typesafe enumeration declarations you can make:
In both cases, there are two primary limitations on this type of customization:
The syntax for the <typesafeEnumClass> customization is:
<typesafeEnumClass [ name = "enumClassName" ] [ <typesafeEnumMember> ... </typesafeEnumMember> ]* [ <javadoc> enumClassJavadoc </javadoc> ] </typesafeEnumClass>
The syntax for the <typesafeEnumMember> customization is:
<typesafeEnumMember name = "enumMemberName"> [ value = "enumMemberValue" ] [ <javadoc> enumMemberJavadoc </javadoc> ] </typesafeEnumMember>
For inline annotations, the <typesafeEnumClass> declaration must be specified in the annotation element of the <simpleType> element. The <typesafeEnumMember> must be specified in the annotation element of the enumeration member. This enables the enumeration member to be customized independently from the enumeration class.
For information about typesafe enum design patterns, see the sample chapter of Joshua Bloch’s Effective Java Programming on the Oracle Technology Network..
The <javadoc> declaration lets you add custom Javadoc tool annotations to schema-derived JAXB packages, classes, interfaces, methods, and fields. Note that <javadoc> declarations cannot be applied globally; they are only valid as sub-elements of other binding customizations.
The syntax for the <javadoc> customization is:
<javadoc> Contents in <b>Javadoc<\b> format. </javadoc>
或者
<javadoc> <<![CDATA[Contents in <b>Javadoc<\b> format ]]> </javadoc>
Note that documentation strings in <javadoc> declarations applied at the package level must contain <body> open and close tags; for example:
<jxb:package name="primer.myPo"> <jxb:javadoc> <![CDATA[<body> Package level documentation for generated package primer.myPo. </body>]]> </jxb:javadoc> </jxb:package>
All standard JAXB binding declarations must be preceded by a namespace prefix that maps to the JAXB namespace URI http://java.sun.com/xml/ns/jaxb. For example, in this sample, jxb: is used. To this end, any schema you want to customize with standard JAXB binding declarations must include the JAXB namespace declaration and JAXB version number at the top of the schema file. For example, in po.xsd for the Customize Inline example, the namespace declaration is as follows:
<xsd:schema xmlns:xsd= "http://www.w3.org/2001/XMLSchema" xmlns:jxb= "http://java.sun.com/xml/ns/jaxb" jxb:version="1.0">
A binding declaration with the jxb namespace prefix takes the following form:
<xsd:annotation> <xsd:appinfo> <jxb:globalBindings binding declarations /> <jxb:schemaBindings> ... binding declarations . ... </jxb:schemaBindings> </xsd:appinfo> </xsd:annotation>
Note that in this example, the globalBindings and schemaBindings declarations are used to specify, respectively, global scope and schema scope customizations. These customization scopes are described in more detail in Scope, Inheritance, and Precedence.
The Customize Inline example illustrates some basic customizations made by means of inline annotations to an XML schema named po.xsd. In addition, this example implements a custom data type converter class, MyDatatypeConverter.java, which illustrates print and parse methods in the <javaType> customization for handling custom data type conversions.
To summarize this example:
To compile and run the Customize Inline example using Ant, in a terminal window, go to the jaxb-ri-install/samples/inline-customize/ directory and type the following:
ant
Key customizations in this sample and the custom MyDatatypeConverter.java class are described in more detail in the following section.
The customized schema used in the Customize Inline example is in the file jaxb-ri-install/samples/inline-customize/po.xsd. The customizations are in the <xsd:annotation> tags.
The following code example shows the globalBindings declarations in po.xsd:
<jxb:globalBindings fixedAttributeAsConstantProperty="true" collectionType="java.util.Vector" typesafeEnumBase="xsd:NCName" choiceContentProperty="false" typesafeEnumMemberName="generateError" bindingStyle="elementBinding" enableFailFastCheck="false" generateIsSetMethod="false" underscoreBinding="asCharInWord"/>
In this example, all values are set to the defaults except for collectionType.
The following code shows the schema binding declarations in po.xsd:
<jxb:schemaBindings> <jxb:package name="primer.myPo"> <jxb:javadoc> <![CDATA[<body> Package level documentation for generated package primer.myPo. </body>]]> </jxb:javadoc> </jxb:package> <jxb:nameXmlTransform> <jxb:elementName suffix="Element"/> </jxb:nameXmlTransform> </jxb:schemaBindings>
The following code shows the class binding declarations in po.xsd:
<xsd:complexType name="PurchaseOrderType"> <xsd:annotation> <xsd:appinfo> <jxb:class name="POType"> <jxb:javadoc> A <b>Purchase Order</b> consists of addresses and items. </jxb:javadoc> </jxb:class> </xsd:appinfo> </xsd:annotation> <!-- ... --> </xsd:complexType>
The Javadoc tool annotations for the schema-derived POType class will contain the description "A <b>Purchase Order</b> consists of addresses and items." The < is used to escape the opening bracket on the <b> HTML tags.
Later in po.xsd, another <javadoc> customization is declared at this class level, but this time the HTML string is escaped with CDATA:
<xsd:annotation> <xsd:appinfo> <jxb:class> <jxb:javadoc> <![CDATA[ First line of documentation for a <b>USAddress</b>.]]> </jxb:javadoc> </jxb:class> </xsd:appinfo> </xsd:annotation>
Of particular interest here is the generateIsSetMethod customization, which causes two additional property methods, isSetQuantity and unsetQuantity, to be generated. These methods enable a client application to distinguish between schema default values and values occurring explicitly within an instance document.
For example, in po.xsd:
<xsd:complexType name="Items"> <xsd:sequence> <xsd:element name="item" minOccurs="1" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity" default="10"> <xsd:annotation> <xsd:appinfo> <jxb:property generateIsSetMethod="true"/> </xsd:appinfo> </xsd:annotation> <!-- ... --> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType>
The @generateIsSetMethod applies to the quantity element, which is bound to a property within the Items.ItemType interface. unsetQuantity and isSetQuantity methods are generated in the Items.ItemType interface.
The class jaxb-ri-install/samples/inline-customize/src/inlinecustomize/primer/MyDatatypeConverter, shown in the following example, provides a way to customize the translation of XML data types to and from Java data types using a <javaType> customization.
package primer; import java.math.BigInteger; import javax.xml.bind.DatatypeConverter; public class MyDatatypeConverter { public static short parseIntegerToShort(String value) { BigInteger result = DatatypeConverter.parseInteger(value); return (short)(result.intValue()); } public static String printShortToInteger(short value) { BigInteger result = BigInteger.valueOf(value); return DatatypeConverter.printInteger(result); } public static int parseIntegerToInt(String value) { BigInteger result = DatatypeConverter.parseInteger(value); return result.intValue(); } public static String printIntToInteger(int value) { BigInteger result = BigInteger.valueOf(value); return DatatypeConverter.printInteger(result); } };
The following code shows how the MyDatatypeConverter class is referenced in a <javaType> declaration in po.xsd:
<xsd:simpleType name="ZipCodeType"> <xsd:annotation> <xsd:appinfo> <jxb:javaType name="int" parseMethod="primer.MyDatatypeConverter.parseIntegerToInt" printMethod="primer.MyDatatypeConverter.printIntTo Integer" /> </xsd:appinfo> </xsd:annotation> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="10000"/> <xsd:maxInclusive value="99999"/> </xsd:restriction> </xsd:simpleType>
In this example, the jxb:javaType binding declaration overrides the default JAXB binding of this type to java.math.BigInteger. For the purposes of the Customize Inline example, the restrictions on ZipCodeType (specifically, that valid United States ZIP codes are limited to five digits) make it possible that all valid values can fit within the Java primitive data type int. Note also that, because <jxb:javaType name="int"/> is declared within ZipCodeType, the customization applies to all JAXB properties that reference this simpleType definition, including the getZip and setZip methods.
The DataType Converter example is similar to the Customize Inline example. As with the Customize Inline example, the customizations in the Datatype Converter example are made by using inline binding declarations in the XML schema for the application, po.xsd.
The global, schema, and package, and most of the class customizations for the Customize Inline and DataType Converter examples are identical. Where the Datatype Converter example differs from the Customize Inline example is in the parseMethod and printMethod used for converting XML data to the Java int data type.
Specifically, rather than using methods in the custom MyDataTypeConverter class to perform these data type conversions, the Datatype Converter example uses the built-in methods provided by javax.xml.bind.DatatypeConverter:
<xsd:simpleType name="ZipCodeType"> <xsd:annotation> <xsd:appinfo> <jxb:javaType name="int" parseMethod="javax.xml.bind.DatatypeConverter.parseInt" printMethod="javax.xml.bind.DatatypeConverter.printInt"/> </xsd:appinfo> </xsd:annotation> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="10000"/> <xsd:maxInclusive value="99999"/> </xsd:restriction> </xsd:simpleType>
To compile and run the DataType Converter example using Ant, in a terminal window, go to the jaxb-ri-install/samples/datatypeconverter/ directory and type the following:
ant
The following sections provide information about binding declaration files:
All JAXB binding declaration files must begin with:
The version, namespace, and schema declarations in bindings.xjb are as follows:
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <jxb:bindings schemaLocation="po.xsd" node="/xs:schema"> <!-- ... binding-declarations ... --> </jxb:bindings> <!-- schemaLocation="po.xsd" node="/xs:schema" --> </jxb:bindings>
An XML file with a root element of <jaxb:bindings> is considered an external binding file. The root element must specify the JAXB version attribute with which its binding declarations must comply; specifically the root <jxb:bindings> element must contain either a <jxb:version> declaration or a version attribute. By contrast, when making binding declarations inline, the JAXB version number is made as an attribute of the <xsd:schema> declaration:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="1.0">
As shown in JAXB Version, Namespace, and Schema Attributes, the namespace declarations in the external binding declarations file include both the JAXB namespace and the XMLSchema namespace. Note that the prefixes used in this example could in fact be anything you want; the important thing is to consistently use whatever prefixes you define here in subsequent declarations in the file.
The fourth line of the code in JAXB Version, Namespace, and Schema Attributes specifies the name of the schema to which this binding declarations file applies, and the schema node at which the customizations will first take effect. Subsequent binding declarations in this file can reference specific nodes within the schema, but this first declaration should encompass the schema as a whole; for example, in bindings.xjb:
<jxb:bindings schemaLocation="po.xsd" node="/xs:schema">
The global schema binding declarations in bindings.xjb are the same as those in po.xsd for the DataType Converter example. The only difference is that because the declarations in po.xsd are made inline, you must embed them in <xs:appinfo> elements, which are in turn embedded in <xs:annotation> elements. Embedding declarations in this way is unnecessary in the external bindings file.
<jxb:globalBindings fixedAttributeAsConstantProperty="true" collectionType="java.util.Vector" typesafeEnumBase="xs:NCName" choiceContentProperty="false" typesafeEnumMemberName="generateError" bindingStyle="elementBinding" enableFailFastCheck="false" generateIsSetMethod="false" underscoreBinding="asCharInWord"/> <jxb:schemaBindings> <jxb:package name="primer.myPo"> <jxb:javadoc> <![CDATA[<body> Package level documentation for generated package primer.myPo.</body>]]> </jxb:javadoc> </jxb:package> <jxb:nameXmlTransform> <jxb:elementName suffix="Element"/> </jxb:nameXmlTransform> </jxb:schemaBindings>
By comparison, the syntax used in po.xsd for the DataType Converter example is:
<xsd:annotation> <xsd:appinfo> <jxb:globalBindings ... binding-declarations ... <jxb:schemaBindings> ... binding-declarations ... </jxb:schemaBindings> </xsd:appinfo> </xsd:annotation>
The class-level binding declarations in bindings.xjb differ from the analogous declarations in po.xsd for the Datatype Converter example in two ways:
<jxb:bindings node="//node-type[@name=’node-name’]">
For example, the following code shows binding declarations for the complexType named USAddress.
<jxb:bindings node="//xs:complexType [@name=’USAddress’]"> <jxb:class> <jxb:javadoc> <![CDATA[ First line of documentation for a <b>USAddress</b>. ]]> </jxb:javadoc> </jxb:class> <jxb:bindings node=".//xs:element [@name=’name’]"> <jxb:property name="toName"/> </jxb:bindings> <jxb:bindings node=".//xs:element [@name=’zip’]"> <jxb:property name="zipCode"/> </jxb:bindings> </jxb:bindings> <!-- node="//xs:complexType [@name=’USAddress’]" -->
Note in this example that USAddress is the parent of the child elements name and zip, and therefore a </jxb:bindings> tag encloses the bindings declarations for the child elements and the class-level javadoc declaration.
The External Customize example is identical to the DataType Converter example, except that the binding declarations in the External Customize example are made by using an external binding declarations file rather than inline in the source XML schema.
The binding customization file used in the External Customize example is jaxb-ri-install/samples/external-customize/binding.xjb.
This section compares the customization declarations in bindings.xjb with the analogous declarations used in the XML schema, po.xsd, in the DataType Converter example. The two sets of declarations achieve precisely the same results.
To compile and run the External Customize example using Ant, in a terminal window, go to the jaxb-ri-install/samples/external-customize/ directory and type the following:
ant