A recent version of the Apache Ant build tool. JiBX download and installation instructions are included in the tutorial. What sets JiBX apart from the others are performance and flexibility features. JiBX performance consistently measures at the top end of the range, surpassing that of other common tools such as JAXB 2.

Author:Kesho Majin
Language:English (Spanish)
Published (Last):12 September 2004
PDF File Size:8.30 Mb
ePub File Size:3.21 Mb
Price:Free* [*Free Regsitration Required]

A recent version of the Apache Ant build tool. JiBX download and installation instructions are included in the tutorial. What sets JiBX apart from the others are performance and flexibility features. JiBX performance consistently measures at the top end of the range, surpassing that of other common tools such as JAXB 2.

JiBX is also more flexible than almost all other Java-XML tools, using binding definitions to decouple the Java structure from the XML representation so that each can be changed independently of the other. With the 1. You can use tools included in the JiBX release to generate a schema definition matching your Java code or to generate Java code matching your schema definition.

Either way, you also get a binding definition that lets you use JiBX to convert between the Java code and XML documents matching the schema definition. Download the latest 1. Installing the tutorial code Now download the tutorial sample code , also provided as a ZIP file. This should create a dwcode2 subdirectory in the jibx directory, with the example files including build. The sample code includes an Ant build file to automate running the JiBX tools and handle the other steps involved in the examples.

If you install the sample code elsewhere, you can still use the Ant build. In this case, you just need to edit the build. Listing 1 shows an abbreviated version of this schema, intended to represent an order from an online store. The full schema is supplied as starter. Listing 1. This is expected to be 12 characters in length, with two leading alpha characters followed by ten decimal digits.

CodeGen tool included in the jibx-tools. You can run the tool directly from the command line or indirectly via a build tool such as Apache Ant. The tutorial download includes an Ant build. To try this out, open a console in the dwcode2 directory of the installed download and type ant codegen. If you have Ant installed on your system and have installed the download code according to the instructions, you should see output similar to that shown in Figure 1: Figure 1. Using the Ant build You can also run CodeGen directly from the console.

To do this, you need to: Include the jibx-tools. Specify org. CodeGen as the class to be run. List the schema definition s to be generated. Generated artifacts The generated code consists of five classes, corresponding to the five global type definitions in the Listing 1 schema.

Listing 2 shows some samples of the generated code, with excerpts from the org. Order class and the entire org. Shipping class: Listing 2. Shipping address information. For repeated values, such as the repeating item element within the Listing 1 order complexType definition, CodeGen generates a Java 5 typed list by default.

For simpleType restriction enumerations, such as the shipping type in Listing 1, CodeGen generates a Java 5 enum type by default. The generated code for both of these instances is shown in Listing 2. The binding compiler adds bytecode to your compiled class files that actually implements the conversions to and from XML, as specified by the binding definition.

The binding compiler is included in the JiBX distribution as part of jibx-bind. The JiBX documentation provides full details about different ways to run the binding compiler, including how you can invoke it when running your application rather than as part of the build.

The build. You can also run all three targets in sequence by listing them in order on the command line: ant codegen compile bind. Figure 2. Marshalling is the process of generating an XML representation for an object in memory, potentially including objects linked from the original object. Unmarshalling is the reverse process of marshalling, building an object and potentially a graph of linked objects in memory from an XML representation.

The Ant run target executes this test program, using the Listing 3 document as input and writing the marshalled copy of the document to a file named out. Listing 4. Test in-file out-file" ; System. Just as in Part 1, the out. A simple customization example CodeGen supports extensive customizations for many aspects of code and binding generation.

The set of customizations to be applied are passed to CodeGen as an XML document, with nested elements that relate to schemas or schema components. Listing 5 gives a simple example: Listing 5. The final pair of attributes work together. CodeGen by default generates a separate top-level class for every global type definition in the schemas specified as input, along with a corresponding abstract mapping in the generated JiBX binding for each complexType.

You can try out these customizations by using the Ant custgen task instead of the codegen task or just use the full task, which runs the complete sequence of targets clean custgen compile bind run.

Listing 6 shows excerpts of the generated code, which you can compare to the default generated code shown in Listing 2. The big differences besides the simplified class Javadocs are that the Customer class is now inlined, and the Shipping class is now an inner class using a custom typesafe enumeration class. Listing 6. It represents more than corporate members, and almost 50 technology firms are certified to meet its standards.

The HR-XML schemas used for this tutorial consist of schemas, including a mixture of top-level document definitions and common components. CodeGen can easily handle this number of schemas, but the number of generated classes and the complexity of the interrelationships would obscure the more interesting aspects of the schema handling.

To focus in on these details, the subset of HR-XML used here consists of a single top-level document definition, for the TimeCard element, along with the common components referenced as part of the TimeCard definition — a total of seven schema definitions.

Listing 7 shows an edited version of the main schema for the TimeCard element definition. The sample directory also contains a test program, org. It unmarshals sample documents using the generated data-model classes and then marshals the documents back out and compares the result with the original document.

The codegen target runs CodeGen using defaults, compile compiles the generated code and test code, bind compiles the JiBX binding, and roundtrip runs the test program on the sample documents. You can also use the full task to run all of these steps in sequence.

Most forms of code generation from schema generate a separate class for each complexType definition and for enumeration simpleTypes. CodeGen often is able to reduce the number of generated classes by examining references and inlining definitions where possible and by ignoring unused definitions in included and imported schema definitions.

In the case of the TimeCard schema, there are a total of 10 global named complexTypes and an additional 23 local anonymous complexTypes, along with 8 enumeration simpleTypes.

CodeGen by default uses a selection variable to track which choice is currently active. The set methods for values included in the choice allow you to write a new value for the current selection but prevent changing the selection directly throwing an IllegalStateException if you try.

To change the current selection once it has been set, you first need to call a clear method here clearReportedResourceSelect which resets the selection state. Listing 8. The choice-exposed customization attribute works in combination with the choice-check settings, which track a current selection state. This allows you to use a Java switch statement easily to execute different code depending on the current state, in place of multiple if statements.

Both these attributes can be used at any level of customization, allowing you to set the behavior for all the generated code on the outermost customization easily while still retaining the ability to do something different on a case-by-case basis. The Element object can be used to represent any arbitrary XML element including all attributes, namespace declarations, and content , so it provides all the flexibility needed to work with any document matching the schema definition.

Listing 9. In this last case, the data model uses java. Object to represent an element, with the actual runtime type of the object matching the global schema definition. You can see in Listing 10 that each of the references to a union type including both the TimeCardDuration type shown in the listing and the AnyDateTimeType is represented by a simple String value in the generated code.

Listing Other transformations are controlled by customizations. Either way, schema fragments included in Javadocs always show the transformed schema because this is what is actually used to generate the code. Customizing the TimeCard data model An example earlier in this tutorial showed some simple CodeGen customizations. Customizing the data model The data-model code generated by CodeGen using default settings has some weaknesses.

For one thing, the schema type names all end with Type, and this carries over to the corresponding generated class names, making the names longer than necessary.

The package name generated from the schema namespace, org. Listing 11 shows another awkward aspect of the generated data-model classes, in which a java. BigInteger is used to represent an xs:integer type. This is the most accurate representation for xs:integer using standard Java classes, but BigInteger is awkward to use compared to simple int primitive or java.

Integer object types. Unfortunately, schemas are often written using the xs:integer type even when xs:int would be more appropriate, so developers can get stuck with BigInteger values in the generated code. You can define multiple pairs of substitutions by just adding more type names to the list, using spaces as separators between the pairs as well as between the type names in each pair. The nested name-converter element configures the handling of XML names being converted to Java names.

You can provide multiple alternatives to be stripped with this attribute, as a space-separated list. You can also use a strip-prefixes attribute to remove unnecessary leading text from names, along with several other forms of customizations. Finally, the nested class-decorator element adds a decorator to the code generation sequence.

In this case, the decorator is a predefined one provided as part of the CodeGen distribution, which adds useful support methods for collection values.


Oh no! Some styles failed to load. 😵

JiBX performs these tasks via utility classes generated at compile time via ant scripts. This approach reduces the processing time by moving away from the traditional two-step process with other parsers to a single step. Benchmarks done on various XML binding tools have shown JiBX as the fastest and the most memory efficient parsing framework till date. You can download these jars or use maven pom.


JiBX Tutorial

Figure 1. The binding definition tells JiBX the details of how this binding is to be performed. The color coding in the diagram shows how the three parts interrelate. You can easily define custom serialization handlers for data values, and you can tell JiBX to call your own methods at various points in the binding process to allow further customizations. To learn more about using these and many other JiBX features check out the Binding Tutorial section of this site. This includes a tool which will generate a default binding for a set of Java classes and a separate tool to generate an XML schema definition from a binding definition and the referenced Java classes. This provides a tool to generate a set of Java classes and a corresponding binding definition from a schema.


JiBX: Getting Started with JiBX


Related Articles