Example usage for javax.xml.bind JAXBException printStackTrace

List of usage examples for javax.xml.bind JAXBException printStackTrace

Introduction

In this page you can find the example usage for javax.xml.bind JAXBException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this JAXBException and its stack trace (including the stack trace of the linkedException if it is non-null) to System.err .

Usage

From source file:Main.java

public static String convertObjectToXML(Object object) {
    JAXBContext jaxbContext = null;
    Marshaller jaxbMarshaller = null;
    StringWriter stringWriter = new StringWriter();
    try {//ww w  .  j a  v a  2s .  co m
        jaxbContext = JAXBContext.newInstance(object.getClass());
        jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        jaxbMarshaller.marshal(object, stringWriter);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    String xmlString = stringWriter.toString();

    return xmlString;

}

From source file:Main.java

public static <T> T unMarshal(String xml, Class<T> clazz, String encoding) {
    T result = null;/*from  ww  w  .j ava2s.com*/
    try {
        ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
        JAXBContext context = JAXBContext.newInstance(clazz);
        result = (T) context.createUnmarshaller()
                .unmarshal(new InputSource(new InputStreamReader(is, encoding)));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return result;

}

From source file:Main.java

public static <T> T loadObject(Class<T> typeClass, URL path) {
    T object = null;//from  w  w w . j  a  va2s.  c o m

    try {
        File file = new File(path.toURI());
        JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        object = (T) jaxbUnmarshaller.unmarshal(file);
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    return object;
}

From source file:Main.java

public static Object unmarshal(String xml, Class<?> classObj) {
    Object obj;/* w  w  w. ja v a2  s .c o  m*/
    try {
        JAXBContext jaxbContext = getJAXBContext(classObj);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        obj = unmarshaller.unmarshal(new StringReader(xml));
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object xmlTobean(String path, String classPath) {
    Object obj = null;// w ww.  j a v a2s .c om
    Class<?> obClass = null;
    try {
        obClass = Class.forName(classPath);
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        File file = new File(path);
        JAXBContext context = JAXBContext.newInstance(obClass);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        obj = unmarshaller.unmarshal(file);
        System.out.println(obj);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlStr, Class<T> t) {
    try {/*w w w  . j a v a 2  s  . c o  m*/
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Object to XML//from  w  ww . j  ava2  s . c o m
 * @param object
 * @return
 */
public static String convertToXML(Object object) {
    try {
        if (!mMap.containsKey(object.getClass())) {
            JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            mMap.put(object.getClass(), marshaller);
        }
        StringWriter stringWriter = new StringWriter();
        mMap.get(object.getClass()).marshal(object, stringWriter);
        return stringWriter.getBuffer().toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String objectToXml(Object object) {
    if (null == object) {
        return NO_RESULT;
    }//from w  ww  .  ja  v  a  2s .c o m

    StringWriter sw = new StringWriter();
    JAXBContext jAXBContent = null;
    Marshaller marshaller = null;

    try {
        jAXBContent = JAXBContext.newInstance(object.getClass());
        marshaller = jAXBContent.createMarshaller();
        marshaller.marshal(object, sw);

        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return NO_RESULT;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlStr, Class<T> t) {
    try {// w w w.j ava2  s. co m
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        T ts = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
        return ts;
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Unmarshaller getUnmarshaller() {
    if (unmarshaller != null) {
        return unmarshaller;
    }//w  w w  . jav a 2s .  com
    try {
        if (jc == null) {
            jc = JAXBContext.newInstance(REQ_PKG);
        }
        unmarshaller = jc.createUnmarshaller();
        return unmarshaller;
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}