Nano is a light weight xml/json binding framework targeting Android platform, in this post, I will show you how to use Nano and the accompanying compiler tool mxjc together to do schema driven xml or json data binding - similar to the development process using JAXB and XJC.
The Big Picture
A picture is worth a thousand words, a typical schema driven xml data binding development includes two main phases:
Build Time - in this phase, we authorize xml scheam or xsd first, the schema specifies the metadata of our business domain, this is the single source of truth, later code generation all depends on the scheam, only if the scheam changes, then we need to regenerate source code. With schema specified, we use the mxjc schema binding compiler to compile the scheam into Nano bindable classes, mxjc is based on JAXB xjc, so its usage is similar to xjc. Classes generated by mxjc will be annotated with Nano annotations, these annotations specify the mapping between pojo and xml, they will later be scanned by Nano binding framework to guide the real marshalling or unmarshalling at runtime.
Runtime time - in this phase, we have Nano bindable domain class in hand, what’s left is to do marshalling or unmarshalling according to real needs, by leveraging Nano binding framework. Nano is a middle man between XML/JSON document and java object, when objects or pojos are fed into the Nano binding framework, it can marshall them or serialize them into either XML or JSON content, and when XML or JSON content is fed into the Nano binding framework, it can unmarshall or deserialize them into objects or pojos.
By the way, scheam driven development is good for some scenario with complex business domain, without formal schema and automated code generation, the manual written code base will be very hard to maintain. However, when your use case is simple, it’s not necessary to begin with scheam, just write your domian class then annotate with Nano annations is also workable.
A Zoo Sample
I’ve create an ant project to demo the scheam driven developmeng with Nano and mxjc, you can find the project here, you can download the whole project and run it with Ant(suppose you have Ant installed).
Belew is the Ant build file which includes all scheam driven development stets mentioned above:
<?xml version="1.0" standalone="yes"?><projectbasedir="."default="run"><propertyname="sample.home"value="."/><pathid="classpath"><pathelementpath="classes"/><filesetdir="${sample.home}"includes="lib/mxjc/*.jar"/><filesetdir="${sample.home}"includes="lib/nano/*.jar"/></path><taskdefname="mxjc"classname="com.leansoft.mxjc.MXJCTask"><classpathrefid="classpath"/></taskdef><!--compile Java source files--><targetname="compile"description="Compile all Java source files"><echomessage="Compiling the schema..."/><mkdirdir="gen-src"/><mxjcschema="schema/zoo.xsd"package="com.leansoft.nano.zoo"destdir="gen-src"removeOldOutput="yes"><producesdir="gen-src/com/leansoft/nano/zoo"includes="**/*.java"/></mxjc><echomessage="Compiling the java source files..."/><mkdirdir="classes"/><javacdestdir="classes"debug="on"><srcpath="src"/><srcpath="gen-src"/><classpathrefid="classpath"/></javac></target><targetname="run"depends="compile"description="Run the sample app"><echomessage="Running the sample application..."/><javaclassname="com.leansoft.nano.sample.ZooExample"fork="true"><classpathrefid="classpath"/></java></target><targetname="clean"description="Deletes all the generated artifacts."><deletedir="gen-src"/><deletedir="classes"/></target></project>
mxjc provides an Ant task called MXJCTask, similar to xjc ant task, mxjc ant task can also auto-generate source from scheam driven by Ant.
packagecom.leansoft.nano.sample;importjava.io.FileInputStream;importjava.util.List;importcom.leansoft.nano.IReader;importcom.leansoft.nano.IWriter;importcom.leansoft.nano.NanoFactory;importcom.leansoft.nano.zoo.Animals;importcom.leansoft.nano.zoo.ZooInfo;importcom.leansoft.nano.zoo.animals.Animal;publicclassZooExample{publicstaticvoidmain(String[]args){try{IReaderxmlReader=NanoFactory.getXMLReader();ZooInfozooInfo=xmlReader.read(ZooInfo.class,newFileInputStream("xml/zoo.xml"));System.out.println("Output after xml read - ");System.out.println("Zoo Name: "+zooInfo.getZooName());System.out.println("Zoo Id: "+zooInfo.getZooId());Animalsanimals=zooInfo.getAnimals();List<Animal>animalsList=animals.getAnimal();for(Animalanimal:animalsList){System.out.println("\t"+animal.getAnimalName());System.out.println("\t\t"+animal.getAnimalType());}IWriterxmlWriter=NanoFactory.getJSONWriter();System.out.println("Output after json write - ");xmlWriter.write(zooInfo,System.out);}catch(Exceptione){e.printStackTrace();}}}
When used properly, scheam driven development can greatly improve agaility in development, and just as I have showed in the post, Nano with mxjc supports scheam driven development smoothly, you may tell me that you would rather choose JAXB to do scheam driven development, I admit that JAXB is more mature and feature-rich than Nano, but one cool thing of Nano is it supports Android platform while JAXB does not, Nano is just tailored for Android platform, in later posts, I will show you how to practice scham driven data binding on Android platform, it’s facinating, stay tuned.