XMLBeam does not do data binding, but a data projection. However, there are three "automatic" types, which provide something similar to data binding.
Sometimes you need getters and setters for a value. The getter has an @XBRead annotation and the setter the @XBWrite with the same path. This way you can read a value from XML and write it back to the same place. The new type XBAutoValue is made to simplify this use case.
public interface Example { @XBRead ( "/foo/@bar" ) XBAutoValue<String> getBar(); } { // read the value String value = example.getBar().get(); // sets the value example.getBar().set( "new value" ); // checks for existence if (example.getBar().isPresent()) { } // removes attribute from foo element example.getBar().remove(); // check for existence and get the value for (String bar:example.getBar()) { // only invoked if attribute is present } // get the name of the element or attribute holding the value String attributeName =example.getBar().getName(); // "bar" in this example // rename the XML attribute 'bar' to 'bar2' in the XML example.getBar().rename( "bar2" ); } |
All operations are applied to the XML DOM model automatically, hence the name.
If you want to access multiple values in an XML sequence, use type XBAutoList. Changes to this list will be applies automatically to the underlying XML just like XBAutoValue.
public interface Projection { @XBRead ( "/root/entries/entry" ) XBAutoList<String> entries(); } { XBAutoList<String> entries = projection.entries(); // get third value of sequence String string = entries.get( 2 ); // set third value of sequence; entries.set( 2 , "entry value" ); // append a new value entries.add( "new value" ); //remove first 5 entries entries.subList( 0 , 5 ).clear(); } |
Just like XBAutoValue, all operations including structure changes are applied to the DOM automatically. An alternative to create an XBAutolist is to use the annotation @XBAuto instead of @XBRead:
@XBAuto ( "/root/entries/entry" ) List<String> entries(); |
public interface Projection { @XBRead ( "/root/foo" ) XBAutoMap<String> entries(); } { XBAutoMap<String> map = projection.entries(); // Read attribute of subelement below /root/foo/.. String attributeValue = map.get( "subelement/@attribute" ); // Create new elements and set value of element 'structure' map.put( "new/sub/structure" , "new value" ); } |
Just like XBAutoList, you may use the @XBAuto annotation with XBAutoMap. Notice, that the map key must be of type String.
@XBAuto ( "/root/foo" ) Map<String,String> entries(); |