This article introduces you to the basics of Java Architecture for XML Binding (JAXB) Early Access Implementation v 1.0. You will learn a few basic uses of the API and tools that the EA v 1.0 provides. This paper provides brief explanations on how to create simple binding codes using the API and tools. In addition, this paper also discusses a few situations where JAXB shows its strengths, and is intended for developers who have working understanding of the Java programming language, are familiar with XML, and interested in getting a brief introduction to JAXB. Introduction to JAXBJava Architecture for XML Binding (JAXB) provides an API and tool that allow automatic two-way mapping between XML documents and Java objects. With a given Document Type Definition (DTD) and a schema definition, the JAXB compiler can generate a set of Java classes that allow developers to build applications that can read, manipulate and recreate XML documents without writing any logic to process XML elements. The generated codes provide an abstraction layer by exposing a public interface that allows access to the XML data without any specific knowledge about the underlying data structure. In addition to DTD support, future versions of JAXB will add support for other schema languages, such as the W3C XML Schema. These features enable the JAXB to provide many benefits for the developers, which are further explained in the next section. A Few Benefits of JAXBThe immediate advantage of JAXB is that it provides a layer of abstraction that enables developers to quickly and conveniently work with XML documents. This advantage is made possible with
For further explanations, please see the Java Architecture for XML Binding User's Guide. Uses of JAXBAn application can take advantage of the benefits that JAXB can offer if a developer wants to do the following:
Developers may discover additional uses of JAXB, and ways to benefit from its features. There are also situations where a different XML processing API may provide additional advantages, such as Java API for XML Processing (JAXP). JAXP provides a SAX parser API, a Document Object Model (DOM) parser API, and an XSL Transformations API. For further information about JAXP API, please refer to the links at the bottom of this document. In the following situations, JAXP can provide these options for developers:
Different features that JAXB and JAXP offer can empower developers to have the option to choose the best API to fit their needs. If the decision is to use JAXB, then the following step-by-step instruction will provide a jump start on the basics of using the JAXB API. Getting StartedBefore starting to work with the JAXB API, it will need to be downloaded and installed. An implementation can be downloaded from the official JAXB page. Once the package is installed, developers will be able to try out the following simple price list example that shows the basics of using the JAXB API. In this example, a list of product names and prices for each product are tracked in XML, which may look like Code Sample 1. JAXB applications implemented to process this XML document can provide additional business logic to satisfy desired functionality. Code Sample 1: Sample XML for the price list example
To build a simple JAXB application to process the above sample XML file, the following steps are used to provide an overview of the development process:
Create or Obtain a DTD
In order to generate XML processing codes, the JAXB compiler requires
a DTD, which defines simple constraints on structure and contents of a
XML document. To keep this example simple, the DTD will define an Code Sample 2: A simple DTD for item list example: item.dtd <!ELEMENT item_list (item_info*)> <!ELEMENT item_info (name, price)> <!ELEMENT name (#PCDATA)> <!ELEMENT price(#PCDATA)> Define a Binding Schema
See Code Sample 3 for a minimal binding schema that binds Code Sample 3: A minimal binding schema: item.xjs
<xml-java-binding-schema>
<element name="item_list" type="class" root="true"/>
<element name="item_info" type="class"/>
</xml-java-binding-schema>
Generate Processing Codes Assuming that the JAXB EA Implementation v 1.0 is installed and set up correctly, run the following command in a command prompt or terminal to generate the processing codes:
Please note that Unmarshal from an XML Document
Unmarshaling is the process of populating a JAXB generated class object with a corresponding XML document. To unmarshal a XML document, call the unmarshal method in a JAXB generated class object with a parameter of
Instantiation
If there is no existing XML document to work with or a new XML document is needed, a new instance of
ItemList il = new ItemList();
ItemInfo ii = new ItemInfo();
ii.setName("Pen");
ii.setPrice("1.99");
List l = il.getContent();
l.add(ii);
If the Work with Data
In addition to the steps described in the previous section to create new instances of JAXB generated classes, there are also other get methods available to retrieve data. In this example, the get methods are Validation
Validation is the process of verifying that the Java object representation of a XML document conforms with the DTD. Validation is required prior to marshaling if content has been added or modified on the Java object representation. To validate the content, just call the
Marshal to a XML document
Marshaling is the process of producing an XML document from Java objects. Marshaling the contents object to a XML document follows a similar process to unmarshaling. The
SummaryThis paper provided a brief overview of the JAXB API. Within this overview, a short list of benefits described the many advantages of using JAXB API, provided an explanation on the use of JAXB, and when an alternative API should be used. To show how a simple JAXB application uses the API, the second half of this paper presented a simple example walkthrough that showed the basics of using the API. By now you should have a basic understanding of the JAXB API and tools in the EA v. 1.0 distribution. With this understanding, you can start writing simple applications that take advantages of the many features and advantages offered by the JAXB. Fore more complex processing techniques, please read the Java Architecture for XML Binding User's Guide. Further Readings and References JAXB
JAXP DTD | ||||||||||||||||||||||||||||||||
|
| ||||||||||||