Java Utililty Methods XML JAXB String to Object

List of utility methods to do XML JAXB String to Object

Description

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

Method

ObjectcreateObject(String xml, Object type)
create Object
JAXBContext jc = JAXBContext.newInstance(type.getClass());
Unmarshaller unmarshaller = jc.createUnmarshaller();
InputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
Object obcj = unmarshaller.unmarshal(stream);
return obcj;
ObjectfromFile(Class jaxbClass, String fileName)
fromFile retrieves object of given class from given file (in classpath)
JAXBContext context = JAXBContext.newInstance(jaxbClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(null);
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
InputStream is = tccl.getResourceAsStream(fileName);
return fromStream(jaxbClass, is);
ObjectfromStream(Class jaxbClass, InputStream is)
fromStream retrieves object of given class from given inputstream
JAXBContext context = JAXBContext.newInstance(jaxbClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(null);
return jaxbClass.cast(unmarshaller.unmarshal(is));
TfromStringToObject(String xmlString, Class xmlObjClass)
from String To Object
return JAXB.unmarshal(new StringReader(xmlString), xmlObjClass);
ObjectfromXml(@SuppressWarnings("rawtypes") Class clazz, String stringXml)
from Xml
JAXBContext context = null;
try {
    context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return unmarshaller.unmarshal(new StreamSource(new StringReader(stringXml)));
} catch (JAXBException e) {
    e.printStackTrace();
return null;
TfromXML(Class clazz, InputStream is)
from XML
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(is);
TfromXml(Class tClass, InputStream inputStream)
from Xml
JAXBContext jaxbContext = null;
try {
    jaxbContext = JAXBContext.newInstance(tClass);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (T) jaxbUnmarshaller.unmarshal(inputStream);
} catch (JAXBException e) {
    e.printStackTrace();
    return null;
...
TfromXml(File xmlPath, Class type)
from Xml
BufferedReader reader = null;
StringBuilder sb = null;
try {
    reader = new BufferedReader(new InputStreamReader(new FileInputStream(xmlPath), ENCODING));
    String line = null;
    sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
...
ObjectfromXML(final byte[] data, final Class type)
Converts byte data representing XML of a JAXB object to an object.
try {
    final JAXBContext context = getContext(type);
    final Unmarshaller unMarshal = context.createUnmarshaller();
    final JAXBElement<?> temp = unMarshal.unmarshal(new StreamSource(new ByteArrayInputStream(data)), type);
    return temp.getValue();
} catch (final JAXBException exception) {
    throw new IllegalStateException(exception);
TfromXml(final Reader reader, final Class dtoClass)
from Xml
Unmarshaller un = null;
try {
    un = getJaxbContext(dtoClass).createUnmarshaller();
    return (T) un.unmarshal(reader);
} catch (JAXBException e) {
    throw new RuntimeException(e);