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> void marshal(JAXBElement<T> element, Class<T> c, OutputStream os) {
    try {/*from   w w  w . jav  a  2s  . c  o m*/
        JAXBContext jc = JAXBContext.newInstance(c);
        Marshaller ma = jc.createMarshaller();
        ma.marshal(element, os);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * XML to Object/*from w  ww .jav  a2 s.com*/
 * @param <T> T
 * @param clazz clazz
 * @param reader reader
 * @return T
 */
@SuppressWarnings("unchecked")
public static <T> T convertToObject(Class<T> clazz, Reader reader) {
    try {
        Map<Class<?>, Unmarshaller> uMap = uMapLocal.get();
        if (!uMap.containsKey(clazz)) {
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            uMap.put(clazz, unmarshaller);
        }
        return (T) uMap.get(clazz).unmarshal(reader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String marshalToString(Class<?> klass, Object obj) throws JAXBException {
    try {//from   ww w .  java  2 s .co  m
        context = JAXBContext.newInstance(klass);
        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        unmarshaller = context.createUnmarshaller();
    } catch (JAXBException e) {
        throw new RuntimeException(
                "There was a problem creating a JAXBContext object for formatting the object to XML.");
    }
    StringWriter sw = new StringWriter();
    String result = null;
    try {
        marshaller.marshal(obj, sw);
        result = sw == null ? null : sw.toString();
    } catch (JAXBException jaxbe) {
        jaxbe.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * Object to XML/*from   www . j a v a 2 s.c  o m*/
 * 
 * @param object
 * @return
 */
public static String convertToXML(Object object) {
    try {
        System.out.println(mMap.containsKey(object.getClass()) + "---mmap-----");
        if (!mMap.containsKey(object.getClass())) {
            JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            // marshaller.setProperty(CharacterEscapeHandler.class.getName(),
            // new CharacterEscapeHandler() {
            // public void escape(char[] ac, int i, int j, boolean
            // flag,Writer writer) throws IOException {
            // writer.write( ac, i, j ); }
            // });
            mMap.put(object.getClass(), marshaller);
        }
        System.out.println("----mmap--" + mMap.toString());
        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 parseObjToXmlString(Object obj) {
    if (obj == null) {
        return noResult;
    }//from  www .java 2s.c o m
    StringWriter sw = new StringWriter();
    JAXBContext jAXBContext;
    Marshaller marshaller;
    try {
        jAXBContext = JAXBContext.newInstance(obj.getClass());
        marshaller = jAXBContext.createMarshaller();
        marshaller.marshal(obj, sw);
        return sw.toString();
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return noResult;
}

From source file:Main.java

/**
 * Writes the content of the specified object into the specified XML file.
 * @param filename            Path to the XML file.
 * @param content            Content as an object of the specified class.
 * @param typeParameterClass   Class of the object with the content.
 * @return                  File.//w  w w  .ja  v  a  2s . c o  m
 */
public static <T> File write(File file, T content, Class<T> typeParameterClass) {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(content, file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return file;
}

From source file:Main.java

/**
 * genXml//from w w w.j a v a2s.  c om
 * 
 * @param o Object Class.
 * @param path : example c:\path\006DS_AMS20130630.xml
 */
public static void genXml(Object o, String path) {
    try {

        // create JAXB context and initializing Marshaller
        JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // for getting nice formatted output
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        //specify the location and name of xml file to be created
        File XMLfile = new File(path);

        // Writing to XML file
        jaxbMarshaller.marshal(o, XMLfile);
        // Writing to console
        jaxbMarshaller.marshal(o, System.out);

    } catch (JAXBException e) {
        // some exception occured
        e.printStackTrace();
    }
}

From source file:Main.java

public static <T> String write(T content, Class<T> typeParameterClass) {
    ByteArrayOutputStream baos = null;
    try {//from w  w w.  j av a 2s.c  o  m
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        baos = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
        jaxbMarshaller.marshal(content, osw);
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return baos != null ? baos.toString() : null;
}

From source file:Main.java

/**
 * Converting DOM Element object into JAXB OASIS XML Object
 * @param <T>/*  w w  w . j a v a2s . c o m*/
 * @param cls
 * @param domElement
 * @return
 */
public static <T> T marshal(Class<T> cls, Element domElement) {
    try {
        JAXBContext jc = JAXBContext.newInstance(cls);
        javax.xml.bind.Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<T> jaxbObject = unmarshaller.unmarshal(domElement, cls);

        T object = jaxbObject.getValue();

        return object;
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:att.jaxrs.util.Marshal.java

public static <T> T unmarshal(Class<T> xmlType, final String xmlString) {
    String namespace = xmlString;
    System.out.println(namespace);

    namespace = namespace.replaceAll(Constants.DATA_SERVICE_XMLNS, "");
    System.out.println(namespace);

    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext;//from   w  ww.j  av a  2s. c o m
    T t = null;
    try {
        jaxbContext = JAXBContext.newInstance(xmlType);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(stream);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return t;
}