Java Model (Foo)
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Foo {
private boolean bar;
public boolean isBar() {
return bar;
}
public void setBar(boolean bar) {
this.bar = bar;
}
}
Demo
import java.io.StringReader;
//from ww w. j a v a 2 s .com
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
public class Main {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader xml = new StringReader("<foo><bar>toast</bar></foo>");
Foo foo = (Foo) unmarshaller.unmarshal(xml);
unmarshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent ve) {
System.out.println(ve.getMessage());
return true;
}
});
System.out.println(foo.isBar());
}
}