Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XML Made Easy with XMLBeans

A new XML parser from BEA makes it easy to handle instance documents in Java, using regular bean-like syntax.


advertisement
very once in a while there comes around a tool so useful it makes you wonder why nobody ever did it before, or why it hasn't been a part of the technology since its inception. Such a tool is XMLBeans, an XML parser developed by BEA for WebLogic 8.1 and recently released as an open source initiative.

We've all used parsers and APIs for handling XML, and there is no shortage of them. XMLBeans is different from the typical XML parser because it is designed to build applications based on known, specific schemas. At its core is a schema compiler that parses an XSD file and creates a JAR. The JAR contains a bunch of Java classes that developers can use to manipulate instance documents as if they were normal Java types.

XMLBeans gives you an object-based view of the underlying XML data, without losing access to the original XML structure. It may appear to be similar to the APIs that allow you to map Java classes to document elements, but it differs in one major aspect: It doesn't take the XML apart in order to bind to its parts. With XMLBeans you handle the entire instance document as a whole, or you can create instance documents in Java by manipulating the classes that have been built for you by the XMLBeans compiler. You do this via regular bean-like access, using get and set methods.



In addition, it gives you an XQuery parser and XMLCursor objects that allow you to query your documents with the flexibility of a SQL database.

Getting Started: Building the Schema
Any XMLBeans project begins with a schema. The point of XMLBeans is to give you a toolkit that allows you to quickly build applications that can efficiently manipulate XML data. Therefore, it is logical to begin with a definition of that data, and that is exactly what an XML schema is. Generally an XML schema is stored in an XSD file.

An example of a very simple schema, which holds name and address details for an individual is shown here.

<?xml version="1.0" encoding="utf-8" ?> <xs:schema targetNamespace="http://tempuri.org/AddressDetails.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/AddressDetails.xsd" xmlns:mstns="http://tempuri.org/AddressDetails.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="AddressDetails"> <xs:complexType id="AD"> <xs:sequence> <xs:element name="Name" type="xs:string" /> <xs:element name="Address1" type="xs:string" /> <xs:element name="Address2" type="xs:string" /> <xs:element name="ZIP" type="xs:string" /> <xs:element name="Email" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

This schema is effectively a template describing how your valid XML should appear. It is a simple case where you have an element that should be called AddressDetails containing a number of child elements called Name, Address1, Address2, ZIP, and Email, each of which are defined as strings (xs:string) for validation purposes.

Author's Note: XML schema support a large number of data types and element types; listing them is beyond the scope of this article. A good place to start investigating XML schema is the W3C's XML schema namespace located at http://www.w3.org/2001/XMLSchema.

The schema above declares the location (http://tempuri.org/AddressDetails.xsd ) of the 'xs' namespace definition, and any element names beginning with 'xs:' belong to the namespace defined at that location. Therefore elements such as 'xs:string' and 'xs:sequence' are defined there, and you may use that as a reference point in trying to understand them. You will be looking at a neat feature of XMLBeans that adds support for these data types a little later on.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap