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 = "http://example.abra.com/helloWorld", preferedPrefix = "ex1") public class HelloWorld { private String name; private String type; private int classID; @MOProperty() public String getName() { return name; } public void setName(String name) { this.name = name; } @MOProperty() public int getClassID() { return classID; } public void setClassID(int classID) { this.classID = classID; } @MOProperty() 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 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(); xmlBindingModel.addBean(HelloWorld.class);
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="http://www.w3.org/2001/XMLSchema" xmlns:ex1="http://example.abra.com/helloWorld" targetNamespace="http://example.abra.com/helloWorld" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="HelloWorld"> <xs:complexType> <xs:sequence> <xs:element name="classID" type="xs:int"/> <xs:element name="name" type="xs:string"/> <xs:element name="type" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
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(); xmlPersister.save(xmlBindingModel, myInstance, "c:/temp/myFile.xml");
And the written XML will for example
<ex1:HelloWorld xmlns:ex1="http://example.abra.com/helloWorld"> <ex1:classID>1231</ex1:classID> <ex1:name>joe</ex1:name> <ex1:type>abc</ex1:type> </ex1:HelloWorld>
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.