Introducing XmlField : Java xml/object mapping framework
• • ☕️ 3 min readWe have recently released XmlField, a new Java, document-oriented xml/object mapping framework.
There are already a lot of tools for this, but none seemed to fit our requirements :- A (very) simple API : we wanted to write as few code/configuration as possible, and keep code meaningful.
- The ability to process and update XML documents without altering additional, unsupported data. This means that the XML model can be updated without requiring to update and deploy existing applications in most cases. (for more information see the “Behavior” page on the projet’s site.
- No need to have a 1:1 mapping between the object XML structure, ability to flatten an XML document into a single object
If you’re writing a standalone application and use XML for storing configuration, XmlField will not be a major improvement (you may still like its simple API).
But if you are working in a corporate environment, with multiple applications sharing data using web or REST services, you may already see the benefits of these points.
A typical use case could be a system where user related data is stored in a central repository as xml documents and where this data is exposed through web services and used by several applications (both for reading and writing).
A some point, a new application requires additional data, which is not currently part of the xml model. This usually requires to update the central repository, adding support for this data through a new version of the web services, then maintain 2 versions of these services or move all existing applications to the new model to get rid of the previous one.
With XmlField-based applications, you just have to create your new app which stores additionnal tags in xml doduments. No need to update the central repository nor other applications. They will continue to process and update Xml objects, ignoring the new data but keeping it safe.
On the technical side, XmlField uses annotated interfaces to map xml data to objects. An interface can be written this way :
@ResourceXPath("/modelRootTag")
public interface IModel {
@FieldXPath("version")
String getVersion();
@FieldXPath("flag")
boolean getFlag();
@FieldXPath("entries/entry")
IEntry getEntries();
void setVersion(String version);
void setFlag(boolean flag);
IEntry addToEntries();
}
Then XmlField can be used this way :
// Source Xml
String xml ="";
// Read doc
XmlField xf = new XmlField();
IModel model = xf.xmlToObject(xmlRessource, IModel.class);
// Play with XML
model.setVersion( "1.0" );
String firstEntryName = model.getEntries()[0].getName();
//Add entry
IEntry newEntry = model.addToEntries();
newEntry.setName( "entry4" );
newEntry.setValue( "value4" );
// Back to XML.
xml = xf.objectToXml( model);
For more detailed examples, I just added a quickstart tutorial and a step-by-step example to the project’s website.
Even if the version number is only 0.6 yet, XmlField is stable enough for most uses and is currently used in production by several projects in different companies. We plan to reach 1.0 after the addition of a new document repository module, providing a lightweight xml document store with search and data validation.
Feedback is welcome. Feel free to test, use and report bugs or patchs using the project bugtracker and mailing lists.
XmlField released under the Apache Licence V2.0
http://xmlfield.sourceforge.net
(Related keywords : xml, java, serialization, marshalling, mapping, binding)