BINDING XML TO JAVA OBJECTS WITH JAXB When using Java as your programming language of choice and XML as your data transport, you might run into issues of how to access XML from within your Java applications. There are many approaches to solving this problem. You might build a custom parser using Sun's Java API for XML Processing (JAXP) or take the poor-man's approach and parse the XML text yourself. One solution you may be less familiar with is using the Java Architecture for XML Binding (JAXB) from Sun. This new technology provides additional benefits previously not available to Java programmers. BACKGROUND The Java community has been facing the issue of how to access XML data for a while. Several XML standards, such as DOM (Document Object Model) and SAX (Simple API for XML), have provided APIs with access to XML documents. The DOM approach has provided a nice API for parsing an XML document and then accessing the data contained in the parser object. SAX takes a more event-driven approach by making calls to functions for particular elements within the document. Each of these technologies meets a different need. DOM is good when you need to parse a document all at once and then access the various parts of it in a more random manner. SAX is good when you only need to access certain parts of a document or you need to parse a particularly large document. Many developers use DOM and SAX to help them transfer data between XML and actual Java classes. By putting XML data directly into Java classes, this simplifies the process of understanding data mapping and accessing particular XML data. The problem with building custom parsers to create custom objects is that everything is so custom, so proprietary. Until the JAXB, there wasn't a standard to define how XML documents should be represented in Java code. This new technology allows developers to take an existing XML schema and turn it into a set of Java classes that represent the XML data. THE DETAILS The JAXB provides the essential functions that take data from an XML document and put it into a Java class, and vice versa. Part of the JAXB is a schema compiler, which understands how to map data from XML entities and elements to Java class members. A Java class represents each element in the schema, and each Java class contains methods for getting and setting object members. The classes also provide functionality for marshalling and unmarshalling XML data. Marshalling is basically the process of taking Java class data and putting it into a serialized format. Unmarshalling is the process of taking serialized data and converting the data into a binary object format (e.g., a Java class). So the basic features of JAXB are to provide a mechanism for coupling XML documents with Java classes that represent the XML documents. The JAXB is not so much an API (like JAXP is) as it is a tool for creating a set of Java classes from an XML schema specification. The schema compiler examines a particular XML schema and creates a set of Java classes that you can use to build a data tree that represents the XML data. Each subelement exists as a member of the elements' corresponding Java class. For example, if you have an element called Address and a subelement called City, then you'll end up with a Java class called Address and a Java class called City. The Address class would contain two methods called getCity() and setCity(), which would get and set the City element within the Address element. Because the City element is part of the Address element, it is automatically marshalled to an XML string when the Address element is marshalled to an XML string. SUMMARY If your Java application needs to access XML data, then JAXB may provide a better approach than traditional DOM or SAX parsers. JAXB includes a schema compiler that creates Java classes, which represent your XML elements. The Java classes are bound to the XML document by get and set functions. For more information about the JAXB, visit the Sun Web site. http://click.online.com/Click?q=e2-J1OpQhyn6RpqJKoENNDWdnh1vGZR Brian Schaffner is a senior consultant for DMR Consulting, a Fujitsu company. He provides architecture, design, and development support for DMR's Telcom360 group. ----------------------------------------