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

TunmarshalFromXml(Class clazz, String xml)
unmarshal From Xml
final JAXBContext context = JAXBContext.newInstance(clazz);
return unmarshalFromXml(context, clazz, xml);
TunmarshalFromXml(final String xml, Class destinationClass)
unmarshal From Xml
JAXBContext jaxbContext = JAXBContext.newInstance(destinationClass);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xml);
return (T) unmarshaller.unmarshal(reader);
TunmarshalInstance(final String data, final Class expectedType)
unmarshal Instance
try {
    final JAXBContext ctx = JAXBContext.newInstance(expectedType);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    final StreamSource streamSource = new StreamSource(new StringReader(data));
    final JAXBElement<T> jaxbElement = unmarshaller.unmarshal(streamSource, expectedType);
    return jaxbElement.getValue();
} catch (JAXBException e) {
    throw new RuntimeException(e);
...
CLAZZunmarshall(Class clazz, String string)
unmarshall
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller um = context.createUnmarshaller();
return (CLAZZ) um.unmarshal(new StringReader(string));
Stringunmarshall(Class c, Object o)
unmarshall
JAXBContext ctx = JAXBContext.newInstance(c);
Marshaller marshaller = ctx.createMarshaller();
StringWriter entityXml = new StringWriter();
marshaller.marshal(o, entityXml);
return entityXml.toString();
Tunmarshall(Class cls, Element domRequest)
Unmarshall from DOM Element to a generic JAXBElement
try {
    JAXBContext jc = JAXBContext.newInstance(cls);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    JAXBElement<T> jaxbObject = unmarshaller.unmarshal(domRequest, cls);
    return jaxbObject.getValue();
} catch (JAXBException e) {
    e.printStackTrace();
return null;
Sunmarshall(InputStream is, Class clz)
unmarshall
JAXBContext context = JAXBContext.newInstance(clz);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<S> root = unmarshaller.unmarshal(new StreamSource(is), clz);
return root.getValue();
Tunmarshall(InputStream toUnmarshall, Class clazz)
UnMarshalls a String to the specidfied object type.
Object unmarshalled = null;
try {
    JAXBContext ctx = JAXBContext.newInstance(clazz.getPackage().getName());
    unmarshalled = ctx.createUnmarshaller().unmarshal(new InputStreamReader(toUnmarshall));
} catch (Exception e) {
    throw new RuntimeException(e);
return (T) unmarshalled;
...
Objectunmarshall(JAXBContext c, Element e)
unmarshall
Unmarshaller u = c.createUnmarshaller();
try {
    u.setEventHandler(null);
    return u.unmarshal(e);
} finally {
    closeUnmarshaller(u);
Objectunmarshall(String cntxtPkg, InputStream in)
unmarshall
Unmarshaller unmarshaller = createUmarshall(cntxtPkg);
if (unmarshaller == null)
    return null;
return unmarshaller.unmarshal(in);