|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TODAY'S TOP SOA & WEBSERVICES LINKS Feature XML Schema Binding with XMLBeans
Parse an XML document and generate an XML document
By: Deepak Vohra
Jan. 4, 2005 12:00 AM
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 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 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 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"); Set the value for the "title" and "author" subelements of the "article" element. Article.setTitle("Service Oriented Architecture Frameworks"); 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 Resources YOUR FEEDBACK
XML JOURNAL LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING XML NEWS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||