Xjc jaxb binding file
Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. The documentation of JAXB xjc says:. Each binding file must have it's own "-b" switch. The syntax of the external binding files is extremely flexible. You may have a single binding file that contains customizations for multiple schemas or you can break the customizations into multiple bindings files:.
In addition, the ordering of the schema files and binding files on the command line does not matter. Customizations to JAXB bindings made by means of an external file containing binding declarations take the general form shown below. Another good resource for information on the external binding file is oreilly. An example binding file from oreilly is:. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to write an external binding file for xjc? If an application-specific datatype is used as the target, your implementation must also provide parse and print methods for unmarshalling and marshalling data.
If you prefer to define your own datatype conversions, JAXB defines a static class, DatatypeConverter , to assist in the parsing and printing of valid lexical representations of the XML Schema built-in datatypes. 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:.
This allows 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 Java Developer Connection. For example, in this sample, jxb: is used.
For example, in po. A binding declaration with the jxb namespace prefix would then take the form:. 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.
In addition, this example implements a custom datatype converter class, MyDatatypeConverter. Key customizations in this sample, and the custom MyDatatypeConverter. The code below shows the globalBindings declarations in po. In this example, all values are set to the defaults except for collectionType. Note: Using typesafe enums enables you to map schema enumeration values to Java constants, which in turn makes it possible to do compares on Java constants rather than string values.
The following code shows the schema binding declarations in po. This customization is useful if a schema uses the same name in different symbol spaces; for example, in global element and type definitions. In such cases, this customization enables you to resolve the collision with one declaration rather than having to individually resolve each collision with a separate binding declaration.
The following code shows the class binding declarations in po. Later in po. See XML 1. 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. The generateIsSetMethod applies to the quantity element, which is bound to a property within the Items.
ItemType interface. In this example, the jxb:javaType binding declaration overrides the default JAXB binding of this type to java. For the purposes of the Customize Inline example, the restrictions on ZipCodeType --specifically that legal US ZIP codes are limited to five digits--make it so all valid values can easily fit within the Java primitive datatype int.
The Datatype Converter example is very 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.
The global, schema, and package, and most of the class customizations for the Customize Inline and Datatype Converter examples are identical. Specifically, rather than using methods in the custom MyDataTypeConverter class to perform these datatype conversions, the Datatype Converter example uses the built-in methods provided by javax. DatatypeConverter :.
The External Customize example is identical to the Datatype Converter example, except that the binding declarations in the External Customize example are made by means of an external binding declarations file rather than inline in the source XML schema. This section compares the customization declarations in bindings. The two sets of declarations achieve precisely the same results. The version, namespace, and schema declarations in bindings.
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 will apply, and the schema node at which the customizations will first take effect. Subsequent binding declarations in this file will reference specific nodes within the schema, but this first declaration should encompass the schema as a whole; for example, in bindings.
The global schema binding declarations in bindings. The only difference is that because the declarations in po. Embedding declarations in this way is unnecessary in the external bindings file.
By comparison, the syntax used in po. The class-level binding declarations in bindings. For example, the following code shows binding declarations for the complexType named USAddress. The Fix Collides example illustrates how to resolve name conflicts--that is, places in which a declaration in a source schema uses the same name as another declaration in that schema namespace collisions , or places in which a declaration uses a name that does translate by default to a legal Java name.
Note: Many name collisions can occur because XSD Part 1 introduces six unique symbol spaces based on type, while Java only has only one. There is a symbols space for type definitions, elements, attributes, and group definitions. As a result, a valid XML schema can use the exact same name for both a type definition and a global element declaration. Like the External Customize example, the Fix Collides example uses an external binding declarations file, binding.
The schema declarations that result in name conflicts are highlighted in bold below. The first conflict in example. Class is a reserved word in Java, and while it is legal in the XML schema language, it cannot be used as a name for a schema-derived class generated by JAXB. However, there is an Ant plugin that we can invoke from Gradle to make generating these classes easy.
Because this is just a Gradle task, it can be included in any project. For this example, we will be using the ISO schemas to generate Java classes. These schemas are especially complex with a single XSD file including hundreds of classes.
To get started, we are going to want to add the jaxb dependency and a basic task with our details:. We want configure the genJaxb task to generate the classes:.
0コメント