1. Home
  2. Tutorials
  3. Java
  4. XML Beans
Yolinux.com Tutorial

Java XML Beans:

Apache framework to generate an XML processing library from an XSD schema

Tutorial on parsing an XML file using XML Beans.

Apache XML Beans Description:

Apache XML Beans is a set of tools and class libraries used to generate a JAR library specifically for an XML schema as defined by an XSD file.

The generated library can be used for XML parsing or XML generation conforming to a specified XSD schema.

JAXB and XML Beans are the two prominent XML parsing and generation frameworks.

  • XML Beans: supports the entire XSD/XML standard. Although it loads the entire document, XML Beans can also parse only the elements and attributes you are looking for.
  • JAXB: loads XML file data into a Java class generated by JAXB as defined by the XSD schema. If the final destination of the data is a class of another definition, the data will have to pass through this intermediate JAXB class.
Both XML Beans and JAXB are reasonably fast. The most significant difference for the developer is in the handling of "mixed types". XML Beans handles this effortlessly with no code changes required for "true" or "false" while JAXB requires different code for each case. See discussion on the mixed content complex type at the end of this tutorial.

This tutorial will cover the use of XML Beans for parsing an XML file.

XML Beans Java parsing library generation process

XML Beans Installation:

Download latest binaries from http://xmlbeans.apache.org/

cd /opt
tar xzf ~/Downloads/xmlbeans-2.5.0.tgz

File: ~/.bashrc
# Apache XMLBeans
if [ -d /opt/xmlbeans-2.5.0 ]
  export CLASSPATH=$CLASSPATH:/opt/xmlbeans-2.5.0/lib/xbean.jar

XML Beans and Java programming require the installation of Java. See our YoLinux Java Tutorial.

Generate a JAR library using XML Beans:

Define an XML schema file (XSD) using your favorite XSD/XML editor:

Generate a Java JAR library file from the XSD: scomp -out corporation.jar corporation.xsd

This will generate the file corporation.jar

Most Java IDE's support the interrogation and use of classes and methods within the JAR file for use by the programmer. You can also employ the following script to view the classes and class methods available:

File: listmeth

echo "List classes and methods available in the JAR file: $1"
# Loop through classes
for class in $(jar -tf $1 | grep '.class'); do
   # Replace "/" with "." to derive class name from path
   # javap - class file disassembler
   javap -classpath $1 ${class//.class/};

Use: listmeth corporation.jar

List classes and methods available in the JAR file: corporation.jar
public final class noNamespace.ETaxStatus$Factory {
  public static noNamespace.ETaxStatus newValue(java.lang.Object);
  public static noNamespace.ETaxStatus newInstance();
  public static noNamespace.ETaxStatus newInstance(org.apache.xmlbeans.XmlOptions);
  public static noNamespace.ETaxStatus parse(java.lang.String) throws org.apache.xmlbeans.XmlException;
  public static noNamespace.ETaxStatus parse(java.lang.String, org.apache.xmlbeans.XmlOptions) ...


Apache XML Beans Example:

File: TestXmlBeans.java

import java.io.File;
import java.util.ArrayList;

import noNamespace.RootDocument;
import noNamespace.RootDocument.Root;
import noNamespace.RootDocument.Root.People.Employee;
import noNamespace.RootDocument.Root.People.Employee.Data;

import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;

public class TestXmlBeans
  static public void main (String[] args) throws XmlException
    String fileName = "CorporationMegaX.xml";

    catch (Exception e)
        System.out.println("Error! Exception caught");

  public static void readFile(String fileName) throws XmlException
        java.io.File inputXMLFile = new java.io.File(fileName);
        RootDocument rootDocument = RootDocument.Factory.parse(inputXMLFile);

        Root root = rootDocument.getRoot();
        System.out.println("Corporation: " + root.getCorporation().getName());

        Employee[] employee = root.getPeople().getEmployeeArray();

        System.out.println("There are " + employee.length + " employees.");

        for (int pp = 0; pp < employee.length; pp++)
            System.out.println("Employee name: " + employee[pp].getName());
            System.out.println("    Gender: "    + employee[pp].getGender().toString());
            System.out.println("    Location: "  + employee[pp].getDesc().toString());
            System.out.println("    Cell phone: "  + employee[pp].getData().getCellPhone());

            String employeeTaxStatus = employee[pp].getTaxStatus().toString();
                System.out.println("    Tax status: W2");
                System.out.println("        Employee number: "     + employee[pp].getUSW2().getEmpNumber());
                System.out.println("        Employee manager: "    + employee[pp].getUSW2().getManager());
                System.out.println("        Employee start year: " + employee[pp].getUSW2().getYearStart());
            else if(employeeTaxStatus.equalsIgnoreCase("US-1099"))
                System.out.println("    Tax status: 1099");
                System.out.println("        SSN number: "        + employee[pp].getUS1099().getSsnNumber());
                System.out.println("        Phone number: "      + employee[pp].getUS1099().getPhone());
                System.out.println("        Corp name: "         + employee[pp].getUS1099().getCorpName());
                System.out.println("        Corp address: "      + employee[pp].getUS1099().getCorpAddress());
                System.out.println("        Corp relationship: " + employee[pp].getUS1099().getRelationship().toString());
                System.out.println("No tax status specified");
    catch (Exception e)
        System.out.println("Error! Exception caught");

XML data file: CorporationMegaX.xml

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="corporation.xsd">
        <Name>Corporation MegaX</Name>
        <Address>512 Megacorp Way, Gotham City</Address>
        <!-- The Boss -->
        <Employee TaxStatus="US-W2" Gender="Male" Desc="OnSite">
            <Name>Mr Grand Kahuna</Name>
                <Address>100 Cherry Hill Lane, Gotham City</Address>
        <!-- The Consultant -->
        <Employee TaxStatus="US-1099" Gender="Male" Desc="OnSite">
            <Name>Mr Special Tee</Name>
                <CorpName>ABC Consulting</CorpName>
                <CorpAddress>3 Mockingbird Lane, Smallville AK</CorpAddress>
                <Address>200 Lookout Hill, Gotham City</Address>
        <!-- The Secratary -->
        <Employee TaxStatus="US-W2" Gender="Female" Desc="OnSite">
            <Name>Mrs Jenny Reliable</Name>
                <Manager>Mr Grand Kahuna</Manager>
                <Address>300 Riverside View, Gotham City</Address>

Ant build script: build.xml

<?xml version="1.0" encoding="utf-8"?>
<project name="IO" default="compile" basedir=".">
        <description>Builds, tests, and runs the project Test.</description>
        <property name="build.dir" value="./" />
        <taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean" classpath="/opt/xmlbeans-2.5.0/lib/xbean.jar" />
        <path id="classpath">
            <pathelement location="/usr/java/latest/lib/tools.jar" />
            <pathelement location="/opt/xmlbeans-2.5.0/lib/xbean.jar" />
            <pathelement location="./corporation.jar" />
        <target name="clean" description="Remove .class files">
            <delete includeEmptyDirs="true" failonerror="false">
                <fileset dir="${build.dir}">
                    <include name="**/*.class" />
        <target name="compile">
            <javac srcdir="./" destdir="./" debug="true" includeAntRuntime="false">
                <classpath refid="classpath" />
                <include name="**/*.java" />
        <target name="run" depends="compile">
            <java classname="TestXmlBeans" failonerror="true" fork="true">
                    <path refid="classpath" />
                    <path location="./"/>

Compile: ant compile

[user1@tux XMLBeans]$ ant compile
Buildfile: /home/user1/Desktop/XMLBeans/build.xml

    [javac] Compiling 1 source file to /home/user1/Desktop/XMLBeans

Total time: 0 seconds

[Potential Pitfall]: If you got the following error:
[javac] Compiling 1 source file to /home/greg/src/Test/JavaXmlBeans
[javac] /home/greg/src/Test/JavaXmlBeans/TestXmlBeans.java:4: error: package noNamespace does not exist
[javac] import noNamespace.RootDocument;
This is due to the XmlBeans JAR file either not being generated or not found in the CLASSPATH. Generate using the command: scomp -out corporation.jar corporation.xsd

Run: ant run

[user1@tux XMLBeans]$ ant run
Buildfile: /home/user1/XMLBeans/build.xml


     [java] Corporation: Corporation MegaX
     [java] There are 3 employees.
     [java] Employee name: Mr Grand Kahuna
     [java]     Gender: Male
     [java]     Location: OnSite
     [java]     Cell phone: 1-800-555-1214
     [java]     Tax status: W2
     [java]         Employee number: 1
     [java]         Employee manager: None
     [java]         Employee start year: 1998
     [java] Employee name: Mr Special Tee
     [java]     Gender: Male
     [java]     Location: OnSite
     [java]     Cell phone: 1-800-555-1216
     [java]     Tax status: 1099
     [java]         SSN number: 123-45-6788
     [java]         Phone number: 1-817-555-1212
     [java]         Corp name: ABC Consulting
     [java]         Corp address: 3 Mockingbird Lane, Smallville AK
     [java]         Corp relationship: CorpToIndividual
     [java] Employee name: Mrs Jenny Reliable
     [java]     Gender: Female
     [java]     Location: OnSite
     [java]     Cell phone: 1-800-555-1218
     [java]     Tax status: W2
     [java]         Employee number: 2
     [java]         Employee manager: Mr Grand Kahuna
     [java]         Employee start year: 1999

Total time: 1 second

Complex Types with Mixed Content:

Our example is for a standard XML file containing only XML content. This is common for configuration files. This is specified in our example by the XML XSD schema:

<xs:element name="US-1099">
  <xs:complexType mixed="false">

One can also have mixed content where there is plain text around the XML content. The XSD would be stated as:

<xs:element name="US-1099">
  <xs:complexType mixed="true">

and the XML would have plain text mixed with the XML as in this example snippet:

SSN Number:     <SsnNumber>123-45-6788</SsnNumber>
Phone Number:   <Phone>1-817-555-1212</Phone>
Corporation:    <CorpName>ABC Consulting</CorpName>
Corp Adress:    <CorpAddress>3 Mockingbird Lane, Smallville AK</CorpAddress>
Relationship:   <Relationship>CorpToIndividual</Relationship>

XML Beans can handle a complexType with a "mixed" text of both "true" and "false" with no change to the code. The same can not be said for JAXB which gets significantly more complex when set to "true".


YoLinux Tutorials:


"Core Java 2, Volume 1: Fundamentals "
by Cay S. Horstmann, Gary Cornell
ISBN # 0132354764, Prentice Hall PTR 8th edition

The industry standard. Need I say more?

"Core Java 2: Volume 2 Advanced Features "
by Cay S. Horstmann, Gary Cornell
ISBN # 0132354799, Prentice Hall PTR 8th edition

The industry standard. Need I say more?

"Core Java Server Faces"
by David Geary, Cay S. Horstmann
ISBN # 0131738860, Prentice Hall PTR 2nd edition

"JSP, Servlets, and MySQL"
by David Harms
ISBN # 0764547879, Hungry Minds, Inc