Fork me on GitHub

Evaluation API

The most common use of XMLBeam is via defining projection interfaces and using the projector.io()... mehtods to read and write these projections. For some use cases you don't need a projection interface at all:

XBProjector projector =new XBProjector();
 
// Read a single String value from file 'filename.xml'
String value = projector.io().file("filename.xml").evalXPath("/some/path").as(String.class);
 
// Read a sequence of integer values
List<Integer> entries = projector.io().file("filename.xml").evalXPath("/some/list/entry").asListOf(Integer.class);
 
// Read xml from url to a map
XBAutoMap<String> map = projector.io().url("http://url").readAsMapOf(String.class);
 
String string = map.get("/some/path"); // returns value at path
int intValue = map.get("/other/path",Integer.class); // returns other value converted to int.
Date date = map.get("/date/path using YYYYMMDD",Date.class); // returns other value converted to Date using a format pattern.
 
//Create document from scratch
Map<String,Object> doc = projector.autoMapEmptyDocument(Object.class);
doc.put("/path/to/value", "value");
doc.put("/path/to/floatValue", 15.0f);
projector.io().file("example.xml").write(doc);

but you can mix the projection and evaluation API seamlessly:

interface Projection {
    @XBRead("./@id")
    int getID();
     
    @XBRead("./entry")
    List<String> entries();
}
XBProjector projector =new XBProjector();
 
// Read a single String value from file 'filename.xml'
List<Projection> list = projector.io().file("filename.xml").evalXPath("/some/path").asListOf(Projection.class);
for (Projection p:list) {
    System.out.println("Entries for ID "+p.getID());
    for (String entry:p.entries()) {               
        //...
    }
}