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

TtoObject(String xml, Class c)
to Object
if (xml == null || "".equals(xml.trim())) {
    return null;
StringReader reader = null;
try {
    JAXBContext context = JAXBContext.newInstance(c);
    javax.xml.bind.Unmarshaller unmarshaller = context.createUnmarshaller();
    reader = new StringReader(xml);
...
TtoObject(String xml, Class clazz)
to Object
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller um = jc.createUnmarshaller();
StringReader reader = new StringReader(xml);
StreamSource source = new StreamSource(reader);
JAXBElement<T> elem = um.unmarshal(source, clazz);
return elem.getValue();
Objectxml2obj(final Class cls, final String xml)
xmlobj
StringReader reader = new StringReader(xml);
JAXBContext contextIn = JAXBContext.newInstance(cls);
Unmarshaller marshallerIn = contextIn.createUnmarshaller();
return marshallerIn.unmarshal(reader);
Txml2Object(String content, Class valueType)
xml Object
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(valueType);
    Unmarshaller um = jaxbContext.createUnmarshaller();
    T t = (T) um.unmarshal(new ByteArrayInputStream(content.getBytes()));
    return t;
} catch (JAXBException e) {
return null;
...
ObjectXml2Object(String xmlString, Class clazz)
Xml Object
if (xmlString == null || "".equals(xmlString.trim())) {
    return null;
Unmarshaller unmarshaller = createUnMarshallerByClazz(clazz);
return unmarshaller.unmarshal(new StringReader(xmlString));
TxmlToJavaBean(String xml, Class c)
xml To Java Bean
if (xml.trim().isEmpty()) {
    return null;
T t = null;
try {
    JAXBContext context = JAXBContext.newInstance(c);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    t = (T) unmarshaller.unmarshal(new StringReader(xml));
...
JAXBElementxmlToJaxb(Class xmlClass, String xml)
xml To Jaxb
JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<?> element;
try (StringReader reader = new StringReader(xml)) {
    element = (JAXBElement<?>) unmarshaller.unmarshal(reader);
return element;
ObjectxmlToJaxb(Class xmlClass, String xml)
xml To Jaxb
JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<?> element;
try (StringReader reader = new StringReader(xml)) {
    element = (JAXBElement<?>) unmarshaller.unmarshal(reader);
return element.getValue();
ObjectxmlToObject(String xml, Class... type)
xml To Object
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(type);
    StringReader stringReader = new StringReader(xml);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return jaxbUnmarshaller.unmarshal(stringReader);
} catch (JAXBException e) {
    e.printStackTrace();
return null;
TxmlToObject(String xml, Class classe)
Transforma o String do XML em Objeto
JAXBContext context = JAXBContext.newInstance(classe);
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller.unmarshal(new StreamSource(new StringReader(xml)), classe).getValue();