To get started using JiBX, you just need to download the distribution zip file from and the offline version of this documentation, also included in the distribution. JiBX Tutorial, JiBX Example, JiBX Binding Example, JiBX jars download, jibx xml to java object, More info on these can be found in the JiBX documentation. You’ll end up with a directory named jibx, which contains all the JiBX JARs, documentation, examples, and even the source code.

Author: Mudal JoJosida
Country: Liberia
Language: English (Spanish)
Genre: Health and Food
Published (Last): 12 December 2004
Pages: 129
PDF File Size: 14.20 Mb
ePub File Size: 2.95 Mb
ISBN: 741-6-41885-776-6
Downloads: 95231
Price: Free* [*Free Regsitration Required]
Uploader: Faezil

But the complexity of its binding definitions and its limited support for increasingly widely used XML schema definitions have frustrated users at times. In this tutorial, you’ll learn about using the new features of JiBX 1.

Part 1 covers the flip side of starting from Java code and generating XML schema definitions. You’ll first learn how to work with a simple schema and generate a default Java data model matching that schema, then use that data model for reading and writing XML documents. You’ll next see how customizations can be used to modify the code generation so it better fits your needs. Finally, you’ll move on to a more complex industry-standard schema example and explore the power of customizations to simplify the data model generated for that schema and improve usability.

After reading this tutorial and working through the supplied examples, you’ll be able to use JiBX to generate customized Java data models for your own schemas. To understand this tutorial, you should have at least a basic knowledge of both Java code and XML.

You don’t need a detailed understanding of XML schema definitions, but some familiarity with schema will help you understand the examples better. 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. 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.

In this tutorial, you’ll see how to apply the second type of generation: You’ll documentwtion to install JiBX before proceeding with this tutorial. Download the latest 1. You’ll end up with a directory named jibx, which contains all the JiBX JARs, documentation, examples, and even the source code. Now download the tutorial sample codealso provided as a ZIP file. This should create a dwcode2 ijbx 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.

Documentaiton learn how in this section. As a simple example, I’ll start with one of the schemas generated in Part 1. 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. 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 View image at full size.

Here’s the Java command line for duplicating the Ant codegen target from a console in the dwcode2 directory assuming you’ve followed the recommended installation instructions:. You can pass many other options to CodeGen from the command line. Documrntation look into those later in the tutorial. Now let’s take a look at the generated Java code.


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. As you can see from Listing documetnationCodeGen automatically converts schema documentation to Javadocs in the generated code shown here as the leading comments in each class Javadoc, and as part of the comments for the getShipTo and setShipTo methods.

For repeated values, such as the repeating item element within the Listing 1 order complexType definition, CodeGen generates a Java jbix 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. Binding definitions contain full documentatiob of the conversions to be done by JiBX, so they’re necessarily complex.

Fortunately, you jibs need to understand the binding definition in order to work with JiBX using CodeGen binding and code generation, so this tutorial doesn’t cover the details. To use the generated binding definition in working with XML documents, you first need to run the JiBX binding compiler. The binding documentahion adds bytecode to your compiled class files that actually implements the conversions to and from XML, as specified by the binding definition.

You must run the binding compiler jivx time you recompile your Java classes or modify the binding definition, so it’s usually best to add the binding-compiler step as part of your project’s standard build process. The binding compiler is included in the JiBX distribution as part documetnation 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.

For this tutorial’s purposes, you’ll keep things simple and just run the binding compiler through Ant.

JiBX: Getting Started

Figure 2 shows the output that you should see when you run these targets, assuming you’ve already run the docuentation targets. You can also run all three targets in sequence by listing them in order on the command line: Listing 3 shows a simple test document matching the schema, included in the tutorial’s code download as starter.

The download package also includes a simple test program, shown here as Listing 4, that demonstrates using JiBX for both unmarshalling and marshalling documents.

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 jibxx 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. This is the same test program as used in Part 1and it’s subject to the same limitations discussed in that tutorial.

Just as in Part 1, the out. In this section, you’ll learn dcumentation basics of customizing CodeGen to control the structure of the code generated from a simple schema. 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 documentatino.

The Listing 5 customization consists of a single, unnamespaced schema element with several different attributes giving the specific customizations to be applied. So far, you’re only working with a single schema docuemntation, so this very simple form of customization can be used.

Later in this tutorial, you’ll see examples of customizations for working with multiple schemas. 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 documenttion task, which runs the complete sequence documenhation targets clean custgen compile bind run.

Listing 6 shows excerpts of docujentation generated code, which you can compare to the default generated code jobx 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 jibbx class. Many additional customizations are available for use with CodeGen.


You’ll see some examples of these later in this tutorial, but to give a better idea of the power of these customizations, it’s necessary to move on to a more complex schema. Working with a stand-alone schema definition is great for a simple demonstration, but it doesn’t give much of a feeling for how a tool functions when applied to the complex schema definitions widely used in enterprise applications.

JiBX , Part 2: XML schema to Java code

Now it’s time to move on to a more realistic example, in the form of one of the industry-standard HR-XML schema definitions. 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.

JiBX: Binding XML to Java Code

This gives a sample of the HR-XML schema style, which uses a mixture of nested and global type definitions and contains a wider range of schema structures than the first example, including:.

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 simpleType s. 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 complexType s and an additional 23 local anonymous complexType s, along with 8 enumeration simpleType s. The generated default data model consists of 15 top-level classes and 23 inner classes, just a few fewer than the number you’d expect to see based on the schema component counts.

You’ll see later some ways of using customizations to further simplify the data model in cases in which not all the schema components are needed. CodeGen by default uses xocumentation selection variable docuemntation 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. For most applications, this type of choice handling works well, preventing documenttion user from trying to set more than one alternative in a choice.

Customizations can be used to modify the default choice handling, though, so if you don’t like this form of choice handling, you can easily change it. 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.