YOUR FEEDBACK
James Nelson wrote: Thanks for the posting, which we are hoping will solve our software issue with t...
AJAXWorld RIA Conference
$300 Savings Expire August 29
Register Today and SAVE!


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TODAY'S TOP SOA & WEBSERVICES LINKS


XML Schema Binding with XMLBeans
Parse an XML document and generate an XML document

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

  • XMLBeans: http://xmlbeans.apache.org/
  • About Deepak Vohra
    Deepak Vohra is a Sun Certified Java 1.4 Programmer and a Web developer.

    YOUR FEEDBACK
    Anatole Tartakovsky wrote: While pricing and peer pressure would propel this device in 100 million units range worldwide within 2 years it is up to AT&T to play hardball with other providers for smartphones market. I used 3G networks since they become available - having plans with Sprint and AT&T. For me ability to tether is critical as I do not care for hotels WiFi that is slower and more expensive then 3G. As a result, I carry 2 phones and move SIM to the one I see fit. First is "Android" linux phone that is a bit less sexy then iPhone but lets me talk on VoIP saving $500/year on the minutes. The second (in laptop bag) is for the times I am inside of clients firewall and need to tether and access outside world without restrictions. I would like to use iPhone as a replacement for both, but it is really up to AT&T to enable tethering plan competitive with "all you can eat" offered by Sprint. Otherwise I will be...
    XML JOURNAL LATEST STORIES . . .
    Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted to be...
    Red Hat CTO Brian Stevens, Citrix CTO Simon Crosby, Egenera CTO Pete Manca, Allen Stewart, Group Manager, Windows Virtualization at Microsoft, and Brian Duckering, Sr. Director of Products and Alliances at Symantec were the top industry executives who joined Jeremy Geelan in the 4th Fl...
    This article is aimed at beginner and intermediate Web developers looking to make the leap into database support of their Web site. The article suggests a new declarative language based on HTML-forms, which is used for development of the database interface. HTML forms can manage not on...
    ISO said Friday that the appeals made by Brazil, India, South Africa and Venezuela protesting the standardization of Microsoft’s Office Open XML (OOXML) file format hadn’t gone anywhere – it was unclear whether any of them had any standing anyway – but since they “failed to g...
    Since its inception, XML has been criticized for the overhead it introduces into the enterprise infrastructure. Business data encoded in XML takes five to 10 times more bandwidth to transmit in the network and proportionally more disk space to store.
    SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
    SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
    Click to Add our RSS Feeds to the Service of Your Choice:
    Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
    myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
    Publish Your Article! Please send it to editorial(at)sys-con.com!

    Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


    SYS-CON FEATURED WHITEPAPERS


    ADS BY GOOGLE
    BREAKING XML NEWS
    Altova® ( http://www.altova.com ), creator of XMLSpy®, the industry leading XML editor, and other ...