The Hello World example

In this example we demonstrate how to create a basic mapped bean and how it is then used to read XML files and write the bean to XML files.

We first write the Bean itself

@MOPersistentBean(xmlName = "HelloWorld")
@MOXmlNamespace(value = "", preferedPrefix = "ex1")
public class HelloWorld {

        private String name;
        private String type;
        private int classID;

        public String getName() {
                return name;

        public void setName(String name) {
       = name;

        public int getClassID() {
                return classID;

        public void setClassID(int classID) {
                this.classID = classID;

        public String getType() {
                return type;

        public void setType(String type) {
                this.type = type;


Note the annotations on the bean class and the properties. * The MOPersistentBean annotation specifies that this is a mapped bean. Any Java class that can be saved to XML or read from an XML document (top level XML element) must be annotated with this annotation.

  • The MOXmlNamespace annotation assigns the XML namespace of the XML document and the generated XML schema.
  • The MOProperty annotation specifies that this is a mapped property. See MOProperty for more options to customize property mapping.

Preparing the environment for XML Persistency

The second step to work with J2XB is to create the MoXmlBindingModel . The MoXmlBindingModel defines persistency environment that we are using - what classes are mapped, what XML Schemas are known and what property editors are defined (for mapping value objects to simple XML Types). It is also responsible for generating XML Schemas from sources.

MoXmlBindingModel xmlBindingModel = new MoXmlBindingModel();

Generating an XML Schema

To generate an XML Schema (or schemas), we use the xmlBindingModel . It exposes two methods to generate the XML Schema -

public void generateSchemas(File dir)
public void generateSchemas(MoXmlFilenameGenerator schemaLocationGenerator)

The first method will write in the specified directory a file per XML Schema contained in the xmlBindingModel using default filename generation. The second method allows controling the XML Schema filenames using the MoXmlFilenameGenerator .

The XML Schema generated for the class above is

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs=""
    elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="HelloWorld">
        <xs:element name="classID" type="xs:int"/>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="type" type="xs:string"/>

Saving an object to XML

When saving an object to XML or reading an object from XML, it is possible to select which XML engine (or binding) framework to use under the hood (implementations of the XmlPersister interface). Corrently, both Apache Axiom and Apache XmlBeans are supported. It is possible to extend the framework with more engines by implementing the interfaces in the xmlBinding package.

To write an object to XML using the Axiom library, we use

HelloWorld myInstance;

XmlPersister xmlPersister = new XmlAxiomPersister();, myInstance, "c:/temp/myFile.xml");

And the written XML will for example

<ex1:HelloWorld xmlns:ex1="">

Reading an object form XML

Reading an XML document into a Java object is also done using the XmlPersister interface. For instance, reading the object saved above using the XMLBeans engine, we use

XmlPersister xmlPersister = new XmlBeansPersister();
HelloWorld myInstance = xmlPersister.load(xmlBindingModel, HelloWorld.class, "c:/temp/myFile.xml");

Note that the XMLBeans implementation of XmlPersister requires that the XMLBeans generated artifacts are in the classpath.