| By Deepak Vohra | Article Rating: |
|
| January 4, 2005 12:00 AM EST | Reads: |
25,785 |
XMLBeans is an open source XML-Java binding tool used to generate Java classes and interfaces from an XML Schema. The generated Java classes may be used to parse or generate an XML document that conforms to the Schema. Some of the advantages of XMLBeans over JAXB are the ability to parse an XML document and support for all of the XML Schema constructs; the JAXB-generated classes do not have a parse method to parse an XML document, and JAXB does not support all of the Schema constructs.
XMLBeans provides the XMLObject, XMLCursor, and SchemaType APIs to parse or construct an XML document with the Java classes and interfaces generated from a schema. In this tutorial, Java classes and interfaces are generated from an example schema. They are used to parse and construct an XML document. The example schema, catalog.xsd, is illustrated in Listing 1.
The example XML document catalog.xml is parsed with the Java classes and interfaces generated with the XMLBeans compiler (catalog.xml is listed in Listing 2). The XMLBeans classes are required to generate Java classes and interfaces from an XML Schema. Obtain the XMLBeans Version 1.03 Binary and Development Kit. Extract the xmlbeans-current.zip to an installation directory. Add <XMLBeans\xmlbeans-1.0.3\bin to the PATH variable. Make <XMLBeans>\xmlbeans-1.0.3\lib\xbean.jar available on the Classpath. <XMLBeans> is the directory in which XMLBeans is installed.
XML Schema Compilation
An XML schema is compiled with the XMLBeans scomp compiler. The scomp parameters and options are listed in Listing 3.
In this section, the example schema catalog.xsd is compiled into Java classes and interfaces with the scomp compiler.
>scomp ?src java catalog.xsd
The ?src option specifies the directory in which the Java classes and interfaces get generated. The scomp compiler might generate the compilation errors shown in Listing 4.
The scomp compiler is a shell script installed in the <XMLBeans>\bin directory that you can modify as needed. The scomp compiler generates a Java class and interface for each of the top-level elements. In the example schema, catalog.xsd, Catalog Document.java, JournalDocument.java, and ArticleDocument.java interfaces get generated in the noNamespace package. Classes CatalogDocument Impl.java, JournalDocumentImpl.java, and ArticleDocumentImpl.java get generated in the noNamespace.impl package. A scomp-compiler-generated interface consists of the getter and setter methods for each of the attributes and subelements of a schema top-level element. A compiler-generated interface also consists of public static final class Factory classes with methods to parse and create an XML document. The scomp compiler also generates a xmltypes.jar file. Add xmltypes.jar to the Classpath. In the following section the example XML document will be parsed with the Java classes and interfaces generated from the example schema.
XML Document Parsing
In this section an example XML document, catalog.xml, is parsed with the classes and interfaces generated with the scomp compiler. Parse catalog.xml with a parse method in the Factory class of the CatalogDocument interface.
CatalogDocument catalogDocument=CatalogDocument.Factory.parse(file);
"file" is specified as a java.io.File object. Obtain a CatalogDocument. Catalog class object from the CatalogDocument object. The Catalog class object corresponds to the <catalog> element in catalog.xml.
CatalogDocument.Catalog catalog=catalogDocument.getCatalog();
Get an array of type JournalDocument.Journal[] from the Catalog object. A Journal class object corresponds to a <journal> element in catalog.xml.
JournalDocument.Journal[] journalArray=catalog.getJournalArray();
Iterate over the Journal[] array to output the "publisher" attribute of the <journal> elements.
for (int i = 0; i < journalArray.length; i++)
{ System.out.println("Journal: " + i);
System.out.println(
" publisher : " + journalArray[i].getPublisher());}
Obtain the ArticleDocument.Article[] array, which corresponds to the <article> elements in a <journal> element.
ArticleDocument.Article[] articleArray=journalArray[i].getArticleArray();
Iterate over the Article[] array and output the attributes and subelements for each of the <article> elements.
The output from parsing an XML document with the XMLBeans-generated classes is illustrated in Listing 5. The Java program, XMLBeansParser.java, used to parse catalog.xml is shown in Listing 6.
In the following section an XML document will be constructed from the Java classes and interfaces generated from the example schema with the XMLBeans compiler.
XML Document Construction
Here an example XML document (catalog.xml) is constructed with the Java classes generated with XMLBeans. Create an object of type CatalogDocument.
CatalogDocument catalogDoc = CatalogDocument.Factory.newInstance();
Add a CatalogDocument.Catalog to the CatalogDocument object. A Catalog represents the <catalog> element in the example XML document that is generated.
CatalogDocument.Catalog catalog=cat alogDoc.addNewCatalog();
Add a JournalDocument.Journal object, which represents a <journal> element, to the Catalog object.
JournalDocument.Journal journal=catalog.addNewJournal();
Set the value for the publisher attribute of the journal element.
journal.setPublisher("IBM developerWorks");
Add an ArticleDocument.Article object, which represents an <article> element, to the Journal object.
articleDocument.Article article=journal.addNewArticle();
Set the "level," "date," and "section" attributes of the <article> element.
article.setLevel("Intermediate");
article.setDate("January-2004");
article.setSection("Java Technology");
Set the value for the "title" and "author" subelements of the "article" element.
Article.setTitle("Service Oriented Architecture Frameworks");
article.setAuthor("Naveen Balani");
Add the other <article> elements to the XML document. The example XML document catalog.xml shown in Listing 2 is generated.
The Java program XMLBeansConstructor.java which was used to construct an example XML document is listed in Listing 7.
Conclusion
Schema binding with JAXB does not support all of the schema constructs, and does not provide parse methods to parse an XML document. XMLBeans? schema-binding supports all of the schema constructs and provides parse methods to parse an XML document, an advantage in binding with the XMLBeans compiler.
Resources
Published January 4, 2005 Reads 25,785
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Deepak Vohra
Deepak Vohra is a Sun Certified Java 1.4 Programmer and a Web developer.
- Publishing Synergy: Blog, Twitter and Ulitzer
- Will PR Firms Survive The New Media Avalanche?
- Typhoon Ondoy (Ketsana) Hits the Philippines (Part 2)
- Confessions of a Ulitzer Addict
- Cloud Computing Expo 2010 East to Attract More Than 5,000 Delegates in New York City
- Cloud Computing Journal Continues To Publish World's Best Cloud Analysts
- CIA Falls for Cloud Computing in a Big Way
- Are You Comfortable With Where Your Data Sleeps at Night?
- Dr. Leslie Lenert of CDC Speaks on Healthcare IT
- Game-Changing Innovations and the Evolving SOA Appliance
- What Happened To SOA?
- Instant Professionalism Online Despite Yourself...with Ulitzer
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Publishing Synergy: Blog, Twitter and Ulitzer
- Will PR Firms Survive The New Media Avalanche?
- Typhoon Ondoy (Ketsana) Hits the Philippines (Part 2)
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Combining the Cloud with the Computing: Application Delivery Networks
- Cloud Computing Expo 2010 East to Attract More Than 5,000 Delegates in New York City
- Ulitzer vs. Ning
- Cloud Computing Journal Continues To Publish World's Best Cloud Analysts
- CIA Falls for Cloud Computing in a Big Way
- Are You Comfortable With Where Your Data Sleeps at Night?
- Where Are RIA Technologies Headed in 2008?
- AJAX World RIA Conference & Expo Kicks Off in New York City
- JSON vs XML - A Jason vs Freddie Sequel
- Processing XML with C# and .NET
- Has the Technology Bounceback Begun?
- BPEL Processes and Human Workflow
- The Top 250 Players in the Cloud Computing Ecosystem
- Open Source Database Special Feature: An Introduction to Berkeley DB XML
- "HP's Problem Ain't the SAP Install," Says Sun's Schwartz
- eXist - An Introduction To Open Source Native XML Database
- Digitizing the Planet: Google Earth vs MSN Virtual Earth vs MapQuest
- Generating XML from Relational Database Tables


































