XMLField is a java XML to Object framework which does not alter additional or not recognized data. This mean you can read and update only a few tags without implementing fully the model and without reading the whole document, just by using setters and getters on real objects.

This framework is based on Dom, Xpath and annotation.


Add maven dependency

	<name>XMLField repository</name>



Define models

public interface IModel {
    String getVersion();

    boolean getFlag();

    IEntry getEntries();

    void setVersion(String version);
    void setFlag(boolean flag);
    IEntry addToEntries();  

public interface IModel {
    String getName();

    String getValue();

    void setName(String name);
    void setValue(boolean value);

The models above map data to the following xml file (example) :

        <entry name="entry1" unsupportedAttribute="content will be preserved">value1</entry>
        <entry name="entry2">value2</entry>
        <entry name="entry3">value3</entry>
   <unsupportedTag>Content will be preserved</unsupportedTag>

Use XmlField

// Source Xml
String xml ="<modelRootTag></modelRootTag>"; 

// 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);

See unit tests from xmlfield-core for more examples.

comments powered by Disqus