Java Utililty Methods XML JAXB Unmarshaller

List of utility methods to do XML JAXB Unmarshaller

Description

The list of methods to do XML JAXB Unmarshaller are organized into topic(s).

Method

UnmarshallergetUnmarshaller()
get Unmarshaller
if (unmarshaller != null) {
    return unmarshaller;
try {
    if (jc == null) {
        jc = JAXBContext.newInstance(REQ_PKG);
    unmarshaller = jc.createUnmarshaller();
...
UnmarshallergetUnmarshaller()
Gets the results Unmarshaller (for validation results.)
try {
    return jaxbContext.createUnmarshaller();
} catch (JAXBException e) {
    throw new RuntimeException("Failed to create Unmarshaller", e);
UnmarshallergetUnmarshaller()
get Unmarshaller
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new ValidationEventHandler() {
    public boolean handleEvent(ValidationEvent event) {
        boolean keepOn = false;
        return keepOn;
});
return unmarshaller;
...
UnmarshallergetUnmarshaller(Class jaxbClass)
get Unmarshaller
JAXBContext jaxbContext = JAXBContext.newInstance(jaxbClass);
return jaxbContext.createUnmarshaller();
TmarshallUnmarshall(T inObj)
Marshalls inObj and unmarshalls the result, returning the unmarshalled version
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
JAXBContext jc = JAXBContext.newInstance(inObj.getClass());
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FRAGMENT, true);
JAXBElement inel = new JAXBElement(new QName("", "obj"), inObj.getClass(), inObj);
m.marshal(inel, doc);
...
Tunmarshal(Class beanClass, InputStream is)
unmarshal
return (T) getUnmarshaller().unmarshal(is);
Stringunmarshal(Class c, Object o)
unmarshal
JAXBContext ctx = JAXBContext.newInstance(c);
Marshaller marshaller = ctx.createMarshaller();
StringWriter entityXml = new StringWriter();
marshaller.marshal(o, entityXml);
String entityString = entityXml.toString();
return entityString;
Tunmarshal(Class clazz, Source source)
unmarshal
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source, clazz);
T object = jaxbElement.getValue();
return object;
Tunmarshal(Class clazz, String xml)
unmarshal
T t = JAXB.unmarshal(new StringReader(xml), clazz);
return t;
Tunmarshal(Class clazz, String xmlInClassPath)
unmarshal
Reader reader = null;
try {
    URL xmlUrl = Resources.getResource(xmlInClassPath);
    reader = new InputStreamReader(xmlUrl.openStream(), "UTF-8");
    JAXBContext context = JAXBContext.newInstance(clazz);
    return (T) context.createUnmarshaller().unmarshal(reader);
} catch (Exception e) {
    throw new RuntimeException("Failed to unmarshal object for class:" + clazz + " xml:" + xmlInClassPath,
...