Listing 1• Catalog.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema">    <xs:element name="catalog">
   <xs:complexType>
    <xs:sequence>
     <xs:element ref="journal" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
   </xs:complexType>
 </xs:element>
 <xs:element name="journal">
   <xs:complexType>
    <xs:sequence>
     <xs:element ref="article" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
      <xs:attribute name="publisher"   type="xs:string"/>
   </xs:complexType>
 </xs:element>
 <xs:element name="article">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="title" type="xs:string"/>
    <xs:element name="author" type="xs:string"/>
   </xs:sequence>
   <xs:attribute name="level" type="xs:string"/>
   <xs:attribute name="date" type="xs:string"/>
   <xs:attribute name="section" type="xs:string"/>
  </xs:complexType>
 </xs:element>
</xs:schema>

Listing 2• Catalog.xml

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <journal  publisher="IBM developerWorks">
    <article level="Intermediate" date="January-2004" section="Java Technology">
        <title>Service Oriented Architecture Frameworks</title>
        <author>Naveen Balani</author>      </article>
    <article level="Advanced" date="October-2003" section="Java Technology"> 
      <title>Advance DAO Programming</title> 
      <author>Sean Sullivan</author>   </article>
  <article level="Advanced" date="May-2002" section="Java Technology">
     <title>Best Practices in EJB Exception Handling</title>
     <author>Srikanth Shenoy </author>  </article>  
</journal>
</catalog>

Listing 3• scomp

Compiles a schema into XML Bean classes and metadata.
Usage: scomp [opts] [dirs]* [schema.xsd]* [service.wsdl]* [config.xsdconfig]*
Options include:
    -cp [a;b;c] - classpath
    -d [dir] - target binary directory for .class and .xsb files
    -src [dir] - target directory for generated .java files
    -srconly - do not compile .java files or jar the output.
    -out [result.jar] - the name of the output jar
    -dl - permit network downloads for imports and includes (default is off)
    -noupa - do not enforce the unique particle attribution rule
    -nopvr - do not enforce the particle valid (restriction) rule
    -compiler - path to external java compiler
    -jar - path to jar utility
    -ms - initial memory for external java compiler (default '8m')
    -mx - maximum memory for external java compiler (default '256m')
    -debug - compile with debug symbols
    -quiet - print fewer informational messages
    -verbose - print more informational messages
    -license - prints license information
    -allowmdef "[ns] [ns] [ns]" - ignores multiple defs in given namespaces

Listing 4• scomp compilation errors

java.lang.NoClassDefFoundError: Beans\xmlbeans-1/0/3\bin\//\build\ar\xbean/jar/;C:\XML
Exception in thread "main" 

'javac' is required on the path.
java.io.IOException: CreateProcess: "C:\javac"

Listing 5• Parsed output from catalog.xml

Catalog has 1 journal elements
Journal: 0
 publisher : IBM developerWorks
Article: 0
Level : Intermediate
Date : January-2004
Section : Java Technology
Title : Service Oriented Architecture Frameworks
Author : Naveen Balani
Article: 1
Level : Advanced
Date : October-2003
Section : Java Technology
Title : Advance DAO Programming
Author : Sean Sullivan
Article: 2
Level : Advanced
Date : May-2002
Section : Java Technology
Title : Best Practices in EJB Exception Handling
Author : Srikanth Shenoy

Listing 6•XMLBeansParser.java

package noNamespace;
import java.io.File;


public class XMLBeansParser 
{
    public  void printElements(File file)
    {
      try{
        CatalogDocument catalogDocument =
            CatalogDocument.Factory.parse(file);

          CatalogDocument.Catalog catalog=catalogDocument.getCatalog();

          JournalDocument.Journal[] journalArray=catalog.getJournalArray();


        System.out.println("Catalog has " + journalArray.length + " journal elements");



     for (int i = 0; i < journalArray.length; i++) 
        {
            System.out.println("Journal: " + i);
            System.out.println(
                " publisher : " + journalArray[i].getPublisher());
            ArticleDocument.Article[] articleArray=journalArray[i].getArticleArray(); 

            for (int j = 0; j < articleArray.length; j++) 
        {
            System.out.println("Article: " + j);
            System.out.println(
                "Level : " + articleArray[j].getLevel());
            System.out.println(
                "Date : " + articleArray[j].getDate());
             System.out.println(
                "Section : " + articleArray[j].getSection());
             System.out.println(
                "Title : " + articleArray[j].getTitle());
             System.out.println(
                "Author : " + articleArray[j].getAuthor());


        } 

        }
}catch(org.apache.xmlbeans.XmlException e){}
 catch(java.io.IOException e){}

    }

public static void main(String[] argv){

XMLBeansParser parser=new XMLBeansParser();
parser.printElements(new File("c:/XMLBeans/catalog.xml"));


  }
}

Listing 7•XMLBeanConstructor.java

package noNamespace;
public class XMLBeansConstructor{

public CatalogDocument createCatalog()
{
    CatalogDocument catalogDoc = 

CatalogDocument.Factory.newInstance();
    CatalogDocument.Catalog 

catalog=catalogDoc.addNewCatalog();
  JournalDocument.Journal 

journal=catalog.addNewJournal();

journal.setPublisher("IBM developerWorks");
ArticleDocument.Article 

article=journal.addNewArticle();
article.setTitle("Service Oriented Architecture 

Frameworks");
   article.setAuthor("Naveen Balani");
article.setLevel("Intermediate");
article.setDate("January-2004");
article.setSection("Java Technology");

 article=journal.addNewArticle();
article.setTitle("Advance DAO Programming");
   article.setAuthor("Sean Sullivan");
article.setLevel("Advanced");
article.setDate("October-2003");
article.setSection("Java Technology");

article=journal.addNewArticle();
article.setTitle("Best Practices in EJB Exception 

Handling");
   article.setAuthor("Srikanth Shenoy");
article.setLevel("Advanced");
article.setDate("May-2002");
article.setSection("Java Technology");

System.out.println(catalogDoc);
    return catalogDoc;

}


public static void main(String[] argv){

XMLBeansConstructor xmlDocument=new 

XMLBeansConstructor();
xmlDocument.createCatalog();


}
}