Java Utililty Methods XML JAXB Unmarshaller

List of utility methods to do XML JAXB Unmarshaller

Description

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

Method

Objectunmarshal(String packageName, InputStream inputStream)
unmarshal
Unmarshaller unmarshaller = createUnmarshaller(packageName);
return unmarshaller.unmarshal(inputStream);
Objectunmarshal(String string, Class clazz)
unmarshal
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return unmarshaller.unmarshal(new StringReader(string));
Tunmarshal(String xml, Class c)
unmarshal
StringReader sr = null;
try {
    sr = new StringReader(xml.toString());
    return JAXB.unmarshal(sr, c);
} catch (DataBindingException e) {
    e.printStackTrace();
    return null;
} finally {
...
Tunmarshal(String xml, Class clazz)
Parse an XML file into a container class.
Unmarshaller unmarshaller = JAXBContext.newInstance(clazz).createUnmarshaller();
return clazz.cast(unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes())));
Tunmarshal(String xml, Class clazz)
unmarshal
return JAXB.unmarshal(new StringReader(xml), clazz);
Stringunmarshal(T entity)
unmarshal
JAXBContext ctx = JAXBContext.newInstance(entity.getClass());
Marshaller marshaller = ctx.createMarshaller();
StringWriter entityXml = new StringWriter();
marshaller.marshal(entity, entityXml);
String entityString = entityXml.toString();
return entityString;
TunmarshalByContent(Class clazz, String xmlContent)
unmarshal By Content
Reader reader = null;
try {
    reader = new StringReader(xmlContent);
    JAXBContext context = JAXBContext.newInstance(clazz);
    return (T) context.createUnmarshaller().unmarshal(reader);
} catch (Exception e) {
    throw new RuntimeException(
            "Failed to unmarshal object for class:" + clazz + " xmlContent:" + xmlContent, e);
...
ElementunmarshalDOMElement(byte[] input)
unmarshal DOM Element
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new ByteArrayInputStream(input));
return doc.getDocumentElement();
ObjectunmarshalFromReader(Reader reader, Class c)
unmarshal From Reader
Object result = null;
JAXBContext jc = JAXBContext.newInstance(c);
Unmarshaller um = jc.createUnmarshaller();
result = um.unmarshal(reader);
return result;
TunmarshalFromString(Class clz, String input)
unmarshal From String
JAXBContext context = JAXBContext.newInstance(clz);
Unmarshaller unmarshaller = context.createUnmarshaller();
T unobj = (T) unmarshaller.unmarshal(new StreamSource(new StringReader(input.toString())));
return unobj;