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.
<repository> <id>xmlField-repository</id> <name>XMLField repository</name> <url>http://xmlfield.sourceforge.net/maven/repository/</url> </repository> (...) <dependency> <groupId>org.xmlfield</groupId> <artifactId>xmlfield-core</artifactId> <version>0.6</version> <scope>compile</scope> </dependency>
IModel.java: @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(); } IEntry.java: @ResourceXPath("/entry") public interface IModel { @FieldXPath("@name") String getName(); @FieldXPath("value") String getValue(); void setName(String name); void setValue(boolean value); }
The models above map data to the following xml file (example) :
<modelRootTag> <version>myVersion</version> <flag>true</flag> <entries> <entry name="entry1" unsupportedAttribute="content will be preserved">value1</entry> <entry name="entry2">value2</entry> <entry name="entry3">value3</entry> </entries> <unsupportedTag>Content will be preserved</unsupportedTag> </modelRootTag>
// 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