ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

XML-Java Data Binding Using XMLBeans XML-Java Data Binding Using XMLBeans

by Hetal C. Shah
07/28/2004

XMLBeans gives an object view of underlying XML data without losing access to the original XML info set, and delivers performance benefits via incremental unmarshalling and efficient methods to access XML schema built-in data types. These two features, along with nearly 100 percent support for XML schema, and provisions for on-time validation of XML data during data manipulation, make XMLBeans very useful for XML-Java data binding.

XMLBeans is currently in the Apache incubation process, and is proving very useful to Java developers for XML-Java binding.

The example code and other files mentioned in this article are available for download from the Resources section below. The examples have been tested with Apache XMLBeans Version 1.02, Java Version 1.4.2_02, and Microsoft Windows 2000.

Creating an XMLBean

Before getting started, download and install Apache XMLBeans Version 1.02 on your system. When you extract the files from the XMLBeans archive, you'll notice bin and lib directories in the distribution. Place the bin directory in your path, and xbean.jar from the lib directory in your classpath.

XMLBeans classes are created from an XML schema file (an XSD file). The resulting XMLBeans classes are able to parse any XML instance document that conforms to the schema. Also, an instance document can be created by manipulating these XMLBeans classes.

For example, the following listing of the weather_latlong.xsd schema describes XML documents containing either weather details or latitude and longitude ("latlong") details of a location, based on zip code.

<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- This XML Schema describes xml documents
containing either weather details or latlong
details of a location based on Zipcode Two Global
elements Weather and Latlong, and one Global
Attribute Zipcode are declared.-->
 <xsd:element name="Weather">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="Temperature"
         type="xsd:float"/>
    <xsd:element name="Humidity"
         type="xsd:float"/>
    <xsd:element name="Visibility"
         type="xsd:float"/>
    <xsd:element name="Datetime"
         type="xsd:dateTime"/>
   </xsd:sequence>
  <xsd:attribute ref="Zipcode"/>
 </xsd:complexType>
 </xsd:element>
 <xsd:element name="Latlong">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="Latitude"
         type="xsd:string"/>
    <xsd:element name="Longitude"
         type="xsd:string"/>
    </xsd:sequence>
   <xsd:attribute ref="Zipcode"/>
  </xsd:complexType>
 </xsd:element>
 <xsd:attribute name="Zipcode"
         type="xsd:string"/>
</xsd:schema>

The next step is to generate a set of XMLBeans classes that represent the above XSD type schema. At a prompt in the working directory where you extracted the files from the example archive, type the following line:

scomp -out weather.jar weather_latlong.xsd

XMLBeans generates following five interfaces when it compiles the above schema: WeatherDocument, WeatherDocument$Weather, LatlongDocument, LatlongDocument$Latlong, and ZipcodeAttribute.

Here, the WeatherDocument interface represents the document element, and the WeatherDocument$Weather interface represents the global element Weather. Similarly, LatlongDocument, and the LatlongDocument$Latlong interface, are for the global element Latlong. The ZipcodeAttribute interface represents the global attribute Zipcode.

XMLBeans Classes

Let's discuss XMLBeans classes in more detail. XMLBeans provides 46 Java types that mirror the 46 built-in types defined by the XML schema specification. For example, where the W3C define an xsd:string, XMLBeans gives you an XmlString data type.

The Weather interface created from weather_latlong.xsd schema has following two methods declared for local element Visibility of type xsd:float:

float getVisibility();

and

org.apache.xmlbeans.XmlFloat xgetVisibility();

For each of these 46 Java types, XMLBeans provides two methods to access the data; here, one method returns an XmlFloat for xsd:float, and another method returns a natural Java type such as float for xsd:float.

The xget version of a function provides a performance benefit over the get version, as the get version has to convert the data to the most appropriate Java type.

Names for schema types become Java-friendly names when the schema is compiled. In other words, a name such as stock-quote becomes StockQuote. In addition, a schema's namespace URIs become package names for XMLBeans types generated from the schema. If the including schema does not have a target namespace declared, then the Java classes generated would all be in the noNamespace package. When there is a name collision of class names, the generated classes will have names with numerals appended -- for example, timeStamp3.

For global elements and attributes, the XMLBeans schema compiler generates an interface whose name ends with Document and Attribute, respectively.

For named types that are declared locally within the declaration of another element or type, XMLBeans generates an inner interface within the interface of element or type in which it is nested.

Consider the following listing of the employee.xsd schema:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This XML Schema describes Employee's
    Jobstatus -->
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:complexType name="Employee">
  <xsd:sequence>
   <xsd:element name="Jobstatus">
    <xsd:simpleType>
     <xsd:restriction base="xsd:NMTOKEN">
      <xsd:enumeration value="fullTime"/>
      <xsd:enumeration value="hourly"/>
     </xsd:restriction>
    </xsd:simpleType>
   </xsd:element>
  </xsd:sequence>
 </xsd:complexType>
</xsd:schema>

Given this, XMLBeans generates an inner interface Jobstatus within the interface of element Employee, in which it is nested.

public interface Employee
  extends org.apache.xmlbeans.XmlObject
{
...
 public interface Jobstatus
   extends org.apache.xmlbeans.XmlNMTOKEN
  {

  }

}

Here, the Employee class extends org.apache.xmlbeans.XmlObject, which is a base interface for all XMLBeans types. All built-in schema types, and user-defined, derived schema types, inherit from XmlObject.

Pages: 1, 2

Next Pagearrow