advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 4.8/5 | Rate this item | 8 users have rated this item.
Email this articleEmail this article
 
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.

  Next Page: Building Your First XMLBean


Page 1: IntroductionPage 3: Using the XMLBean to Read XML Data
Page 2: Building Your First XMLBean 
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES