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

TinputStreamToObject(InputStream xml, Class clazz)
input Stream To Object
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
try {
    return (T) unmarshaller.unmarshal(xml);
} finally {
    xml.close();
TinputStreamToObject(String xml, Class clazz, Class[] classes)
input Stream To Object
InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
JAXBContext jaxbContext = JAXBContext.newInstance(classes);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
T unmarshal = (T) unmarshaller.unmarshal(stream);
return unmarshal;
Eload(Class clazz, InputStream is)
load
JAXBContext jaxbContext;
Unmarshaller jaxbUnmarshaller;
jaxbContext = JAXBContext.newInstance(clazz);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
@SuppressWarnings("unchecked")
E result = (E) jaxbUnmarshaller.unmarshal(is);
return result;
Eload(Class clazz, InputStream is)
load
JAXBContext jaxbContext;
Unmarshaller jaxbUnmarshaller;
jaxbContext = JAXBContext.newInstance(clazz);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
@SuppressWarnings("unchecked")
E result = (E) jaxbUnmarshaller.unmarshal(is);
return result;
Tload(File f, Class... args)
load
return load(new FileInputStream(f), args);
Tload(final String xml, final Class clazz)
load
StringReader reader = new StringReader(xml);
try {
    final JAXBContext context = JAXBContext.newInstance(clazz);
    final Unmarshaller um = context.createUnmarshaller();
    @SuppressWarnings("unchecked")
    final T resource = (T) um.unmarshal(reader);
    return resource;
finally {
    reader.close();
Tload(InputStream input, Class type)
load
try {
    JAXBContext context = JAXBContext.newInstance(type);
    Unmarshaller um = context.createUnmarshaller();
    return (T) um.unmarshal(input);
} catch (JAXBException ex) {
    throw new IOException("Exception thrown while deserializing stream", ex);
TloadObject(Class typeClass, URL path)
load Object
T object = null;
try {
    File file = new File(path.toURI());
    JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    object = (T) jaxbUnmarshaller.unmarshal(file);
} catch (JAXBException e) {
    e.printStackTrace();
...
TloadObject(Path path, Class clazz)
Loads the object from the file.
T result;
if (path == null || clazz == null) {
    throw new RuntimeException("The path to file or class is null!");
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    result = (T) jaxbUnmarshaller.unmarshal(path.toFile());
...
TloadXML(Class type, File sourceFile)
load XML
try {
    JAXBContext context = JAXBContext.newInstance(type);
    return (T) context.createUnmarshaller().unmarshal(sourceFile);
} catch (JAXBException e) {
    throw new RuntimeException(e);