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

ObjectconvertStringToJAXB(String str)
Converts a String object to a JAXB object.
Unmarshaller u = jaxbContext.createUnmarshaller();
return u.unmarshal(new StreamSource(new StringReader(str)));
JAXBExceptionconvertToJAXBException(String msg, Throwable e)
Coverts the passed exception into a new instance of javax.xml.bind.JAXBException .
return new JAXBException(msg, e);
TconvertToObject(Class clazz, InputStream inputStream)
XML to Object
return convertToObject(clazz, new InputStreamReader(inputStream));
voidconvertToXmlFile(File file, Object source, Class... type)
convert To Xml File
System.out.println("Generando Archivo: " + file.getName());
System.out.println("#############################");
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(type);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(source, file);
    marshaller.marshal(source, System.out);
...
ObjectconvertXmlFileToObject(Class clazz, String xmlPath)
convert Xml File To Object
Object xmlObject = null;
try {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    FileReader fr = null;
    try {
        fr = new FileReader(xmlPath);
    } catch (FileNotFoundException e) {
...
ObjectconvertXmlToObj(Class clazz, String xmlStr)
convert Xml To Obj
try {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return unmarshaller.unmarshal(new StringReader(xmlStr));
} catch (JAXBException e) {
    e.printStackTrace();
return null;
...
TconvertXMLToObject(byte[] data, Class clazz)
convert XML To Object
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(clazz.getPackage().getName());
Unmarshaller um = jaxbCtx.createUnmarshaller();
JAXBElement<T> element = (JAXBElement<T>) um.unmarshal(new ByteArrayInputStream(data));
return element.getValue();
TconveryToJavaBean(String xml, Class c)
convery To Java Bean
T t = null;
try {
    JAXBContext context = JAXBContext.newInstance(c);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
    e.printStackTrace();
return t;
TconveryToJavaBean(String xml, Class c)
convery To Java Bean
T t = null;
try {
    JAXBContext context = JAXBContext.newInstance(c);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
    e.printStackTrace();
return t;
TconveryToJavaBean(String xml, Class clazz)
convery To Java Bean
T t = (T) JAXB.unmarshal(new StringReader(xml), clazz);
return t;