The annotation process is intended to be as simple as possible. It is performed mostly with the Bind annotation, providing the XPath expressions and complementary configuration.
First Steps
Start by importing the Bind Class.
import net.vionta.xml.xesta.bind.annotation.Bind;
The next step is to provide an XPath expression for the main class.
@Bind(expression="*:appender")public class Appender implements Serializable {
Each annotated class MUST:
-
Implement the Serializable interface.
-
Provide a no argument constructor
public Appender() {}
Annotating Simple Properties
Properties should be also marked with the Bind annotation.
@Bind(expression = "@*:name")private String name;
@Bind(expression = "*:param/@value")private String level;
You can mark both elements, element text content or attributes. The data type is inferred from the Java type. By now the following Java types are supported:
-
Integer
-
Short
-
Float
-
Double
-
Long
-
Byte
For each property mapping regular setters and getters should be provided :
public String getName() {return name;}
public void setName(String name)
Providing Date Formats for dates.
Mapping netsted objects
Mapping nested objecs is quite similar to map regular properties. You just point the mapping expression to the Element that acts as the base context of the element properties.
In the following example we define a class (Location) with a nested "Group" Class. The Group class is mapped to an Xml "Band" element that has no namespace. Keep in mind that namespace declarations are not optional in XPath 3 and later.
@Bind(expression = "/concert-list/shows/Concert")public class Location implements Serializable {
@Bind(expression = "./event/Band")private Group group;
@Bind(expression = "./name")private String name;
You must add the usual setters and getters and a no argument constructor. Also the element should implement the Serialzable interface. If the Group class can be simply completed by adding the property mappings.
@Bindpublic class Group implements Serializable {
@Bind(expression="./name")private String name;
@Bind(expression = "./event")private Event event;
Upward and downward element exploration
The class element mapping acts as a base level for the property mappings but properties are not limited to the element direct child elements. XPath navigation can be defined downward, upward or using axes and filters. This opens lots of possibilities on the read side but also faces some limitations when it comes to serialization, as the algorithm may not have enought information to determine where to put the information.
Downward examples
Let's start with an open office spreadsheet. We can map a Sheet class to the first spread sheet element of the file:
@Bind(expression = "//office:body/office:spreadsheet",namespaceAlias = {"office"} ,namespaceUris = {"urn:oasis:names:tc:opendocument:xmlns:office:1.0"})public class Sheet implements Serializable {
@Bind(namespaceAlias = {"table"} , namespaceUris = {"urn:oasis:names:tc:opendocument:xmlns:table:1.0"})private List
Jump to content