Welcome!

XML Authors: Katharine Hadow, Greg Schulz, Ambal Balakrishnan, Jeff Scholes, Brad Abrams

Related Topics: XML

XML: Article

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/
  • More Stories By Deepak Vohra

    Deepak Vohra is a Sun Certified Java 1.4 Programmer and a Web developer.

    Comments (0)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.