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

TfromXml(InputStream input, Class clazz)
from Xml
try {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return (T) unmarshaller.unmarshal(new InputStreamReader(input, CHARSET));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (JAXBException e) {
    e.printStackTrace();
...
TfromXml(InputStream xml, Class objectClass)
Deserialize the XML InputStream into an instance of provided class
JAXBContext context = JAXBContext.newInstance(objectClass);
T obj = (T) context.createUnmarshaller().unmarshal(xml);
return obj;
TfromXml(Reader xml, Class clazz)
Converts an input XML reader into the given type.
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(xml);
TfromXml(String responseBody, Class c)
from Xml
ByteArrayInputStream inputStream = new ByteArrayInputStream(responseBody.getBytes());
JAXBContext jaxbContext = JAXBContext.newInstance(c);
XMLStreamReader xsr = xmlInputFactory.createXMLStreamReader(inputStream);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (T) jaxbUnmarshaller.unmarshal(xsr);
TfromXml(String xml, Class clazz)
from Xml
try {
    StringReader reader = new StringReader(xml);
    return (T) createUnmarshaller(clazz).unmarshal(reader);
} catch (JAXBException e) {
    throw new RuntimeException(e);
TfromXml(String xml, Class type)
from Xml
try {
    return (T) JAXBContext.newInstance(type).createUnmarshaller()
            .unmarshal(new ByteArrayInputStream(xml.getBytes()));
} catch (JAXBException e) {
    throw new RuntimeException(e);
TfromXML(String xml, Class valueType)
from XML
try {
    JAXBContext context = JAXBContext.newInstance(valueType);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
    throw new RuntimeException(e.getMessage());
ObjectgetXmlObject(String xml, Class cls)
get XML object for the xml string.
return JAXB.unmarshal(new StringReader(xml), cls);
ObjectstringToObject(String s, Class theclass)
string To Object
Object o = null;
ByteArrayInputStream input = new ByteArrayInputStream(s.getBytes());
JAXBContext jaxbContext = JAXBContext.newInstance(theclass);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
o = theclass.cast(jaxbUnmarshaller.unmarshal(input));
return o;
ObjecttoObject(Class className, String strXml)
to Object
Object object = null;
StringReader reader = null;
try {
    reader = new StringReader(strXml);
    JAXBContext context = JAXBContext.newInstance(className);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    object = unmarshaller.unmarshal(reader);
} catch (Exception e) {
...