Getting Started

Xanot Library

You can get lattest xanot distribution on the download page.

How It Works

Assuming the following XML file parent-child.xml:

<parent>
	<child tagname="child1">
		<desc>This is description of Child #1</desc>
		<child tagname="child11">

			<desc>This is description of Child #11</desc>
		</child>
		<child tagname="child12">
			<desc>This is description of Child #12</desc>

		</child>
	</child>
	<child tagname="child2">
		<desc>This is description of Child #2</desc>

		<child tagname="child21">
			<desc>This is description of Child #21</desc>
		</child>
		<child tagname="child22">

			<desc>This is description of Child #22</desc>
		</child>
	</child>
</parent>

This is a very simple xml data showing a parent-child relations. If you dont know what XML is... Hey, why are you reading this page ?

Now you want to map this XML into these java objects :

parent.java

package org.xanot;

import org.xanot.annotation.TagInstance;
import org.xanot.annotation.XmlCollectionProperty;


import java.util.ArrayList;


@TagInstance(path = "parent")
public class Parent {
  private ArrayList<Child> childs = new ArrayList<Child>();

  @XmlCollectionProperty(methodName = "addChild", xmltag = "child", element = Child.class)

  public ArrayList<Child> getChilds() {
    return childs;
  }

  public void setChilds(ArrayList<Child> childs) {
    this.childs = childs;
  }

  public void addChild(Child child) {
    childs.add(child);
  }
}

child.java

package org.xanot;

import org.xanot.annotation.ParentReference;
import org.xanot.annotation.Repeatable;
import org.xanot.annotation.TagInstance;
import org.xanot.annotation.XmlAttribute;

import org.xanot.annotation.XmlCollectionProperty;
import org.xanot.annotation.XmlSingleProperty;

import java.util.ArrayList;

@TagInstance(path = "child")
@Repeatable
public class Child {
  private Parent parent = null;
  private String name = null;
  private String description = null;
  private ArrayList<Child> childs = new ArrayList<Child>();

  @ParentReference

  public Parent getParent() {
    return parent;
  }

  public void setParent(Parent parent) {
    this.parent = parent;
  }

  @XmlAttribute(name = "tagname")
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @XmlSingleProperty(name = "desc")

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  @XmlCollectionProperty(methodName = "addChild", xmltag = "child", element = Child.class)
  public ArrayList<Child> getChilds() {
    return childs;
  }

  public void setChilds(ArrayList<Child> childs) {
    this.childs = childs;
  }

  public void addChild(Child child) {
    childs.add(child);
  }
}

Both objects, is a simple POJO. This means that these object is a very common java model consisting of simple member attributes with it's getter and setter methods. Ofcourse you could create a much more complex object model and work with Xanot.

As you can see on both java object, it uses specific annotations on the class and method levels. These are Xanot's annotations. It denotes how to maps an xml into this objects. Xanot will read the java class through reflection and discover these annotation when building the mapping rules.

Mapping the XML to the object model is as simple as :

    Xanot xanot = new Xanot();
    xanot.setRoot(Parent.class);
    Parent parent = (Parent) xanot.parse("/path/to/parent-child.xml");

Yup, it's easy. The return of xanot.parse method is a mapped instance of the model. It contains all data from the xml.