Java Utililty Methods XML JAXB Unserialize

List of utility methods to do XML JAXB Unserialize

Description

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

Method

Tread(File file, Class typeParameterClass)
Reads an XML file and returns the content as an object of the specified class.
T content = null;
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    content = (T) jaxbUnmarshaller.unmarshal(file);
} catch (JAXBException e) {
    e.printStackTrace();
return content;
StringreadComplexProperty(String name, List objects, String methodName)
read Complex Property
for (Object o : objects) {
    if (o instanceof JAXBElement) {
        JAXBElement element = (JAXBElement) o;
        if (name.equals(element.getName().getLocalPart())) {
            return callMethod(element.getValue(), methodName);
return null;
ObjectreadExternal(InputStream inputStream, Class clazz)
Reads the InputStream of JAXB marshaled xml and creates an object of the given Class
Object object = null;
try {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    object = (Object) unmarshaller.unmarshal(inputStream);
} catch (Exception e) {
    e.printStackTrace();
return object;
TreadJAXB(Class clazz, InputStream is)
read JAXB
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller u = jc.createUnmarshaller();
return (T) u.unmarshal(is);
TreadJaxbObject(InputStream inputStream, Class jaxbModelClass)
read Jaxb Object
JAXBContext context = JAXBContext.newInstance(jaxbModelClass.getPackage().getName());
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setEventHandler(new DefaultValidationEventHandler());
return unmarshaller.unmarshal(new StreamSource(inputStream), jaxbModelClass).getValue();
TreadObject(Class clazz, File file)
read Object
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (T) unmarshaller.unmarshal(file);
TreadObjectFromInputStream(final InputStream inputStream, final Class expectedType)
read Object From Input Stream
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(expectedType);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    JAXBElement<T> element = unmarshaller.unmarshal(new StreamSource(inputStream), expectedType);
    return element.getValue();
} catch (final JAXBException e) {
    throw new RuntimeException("Cannot process resource!", e);
TreadObjectFromXml(Class jaxbBindClass, String xmlFileName)
read java object from XML.
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(xmlFileName);
return readObjectFromXml(jaxbBindClass, is);
JAXBElementreadXmlFileToObj(String path, String packageName)
read Xml File To Obj
File file = new File(path);
JAXBContext jContext = JAXBContext.newInstance(packageName);
Unmarshaller unmarshaller = jContext.createUnmarshaller();
return (JAXBElement<?>) unmarshaller.unmarshal(file);
Stringunserialize(T component, Class returnType)
unserialize
try {
    Marshaller marshaller = JAXBCONTEXT.createMarshaller();
    StringWriter sw = new StringWriter();
    marshaller.marshal(component, sw);
    return sw.toString();
} catch (JAXBException je) {
    return "failed";