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 <T> String asXml(T object) {
    try {/*from ww  w  . j  a  va2 s. com*/
        JAXBContext jaxbContext = null;
        jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        return writer.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

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

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToObject(String str, Class<T> clszz) {
    JAXBContext context = null;//from   www  . ja v a 2  s  . c om
    T obj = null;

    try {
        context = JAXBContext.newInstance(clszz);
        StringReader reader = new StringReader(str);
        Unmarshaller unmar = context.createUnmarshaller();
        obj = (T) unmar.unmarshal(reader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

public static String marshaller(Object o, Class<?> T) {
    JAXBContext jc;/*from  w w w . jav a2  s.  c om*/
    Marshaller marshaller;
    StringWriter writer = new StringWriter();
    try {
        jc = JAXBContext.newInstance(T);
        marshaller = jc.createMarshaller();
        marshaller.marshal(o, writer);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return writer.toString();

}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshaller(String xml, Class<?> T) {
    JAXBContext jc;/*from   w ww. j  a  v a 2s .  com*/
    Unmarshaller unmarshaller;
    Object o = null;
    try {
        jc = JAXBContext.newInstance(T);
        unmarshaller = jc.createUnmarshaller();
        o = unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return (T) o;
}

From source file:Main.java

public static String parseObjectToXml(Object obj) {
    if (obj == null) {
        return NO_RESULT;
    }//from  w  ww  .j a  va 2  s .co  m
    StringWriter sw = new StringWriter();
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(obj, sw);
        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return NO_RESULT;
}

From source file:Main.java

public static String toXML(Object obj) {
    try {//from w w w .j a  v a2  s. c o m
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static <T> T xmlToObj(Class<T> t, String xml, String encoding) {
    try {/*from   ww w.j a  va2  s .co  m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(encoding));
        @SuppressWarnings("unchecked")
        T obj = (T) unmarshaller.unmarshal(bais);
        bais.close();
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object xml2Bean(Class<?> zClass, String xml) {
    Object obj = null;//from w w w . j a  va 2  s.c  o m
    JAXBContext context = null;
    if (null == xml || "".equals(xml) || "null".equalsIgnoreCase(xml) || xml.length() < 1)
        return obj;
    try {
        context = JAXBContext.newInstance(zClass);
        InputStream iStream = new ByteArrayInputStream(xml.getBytes());
        Unmarshaller um = context.createUnmarshaller();
        obj = (Object) um.unmarshal(iStream);
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

private static String parseObject2XmlString(Object object) {
    if (object == null) {
        return noResult;
    }//from  w  w w .  j a  v  a2  s  .  co  m
    StringWriter sw = new StringWriter();
    JAXBContext jaxbContext;
    Marshaller marshaller;
    try {
        jaxbContext = JAXBContext.newInstance(object.getClass());
        marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(object, sw);
        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
        return noResult;
    }
}