Example usage for javax.xml.bind JAXBContext createMarshaller

List of usage examples for javax.xml.bind JAXBContext createMarshaller

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext createMarshaller.

Prototype

public abstract Marshaller createMarshaller() throws JAXBException;

Source Link

Document

Create a Marshaller object that can be used to convert a java content tree into XML data.

Usage

From source file:Main.java

public static String toXxml(Object bean) {
    StringWriter stringWriter = null;
    try {/*from w w w.  j a  va 2 s .  co m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(bean.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        stringWriter = new StringWriter();
        marshaller.marshal(bean, stringWriter);
        String result = stringWriter.toString();
        // remove xml declaration
        result = result.replaceFirst(".*\n", "");
        return result;
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    } finally {
        if (stringWriter != null)
            try {
                stringWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

From source file:Main.java

public static String toString(Object entity, boolean formatOutput) {

    StringWriter stringWriter = new StringWriter();

    try {//from   w w  w. j  a v a 2s.  c  o m

        Map<String, Object> properties = new HashMap<String, Object>();

        properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, formatOutput);

        JAXBContext jaxbContext = JAXBContext.newInstance(entity.getClass());

        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.marshal(entity, stringWriter);

    } catch (JAXBException e) {
        stringWriter.write(e.getMessage());
    }
    return stringWriter.toString();
}

From source file:Main.java

/**
 * this method is responsible for converting any pojo to respective xml form
 * /*from ww w . java 2s  . c  o m*/
 * @param object
 * @param filePath
 * @throws JAXBException
 * @throws IOException
 */
public static File pojoToXml(Object object, String filePath) throws JAXBException, IOException {

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    OutputStream os = new FileOutputStream(filePath);
    marshaller.marshal(object, os);
    File file = new File(filePath);
    return file;
}

From source file:Main.java

/**
 * Object to XML//from ww w . j  a  va 2  s.co  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

/**
 * //  w  w  w.j av  a2s . c  om
 * @param object
 * @return
 * @throws JAXBException
 */
public static String pojoToXml(Object object) throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // marshaller.setProperty(Marshaller.JAXB_ENCODING, "U");
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);
    String xmlData = writer.toString();
    return xmlData;
}

From source file:Main.java

public static <T> void saveObject(T object, Class<T> typeClass, String path) {
    try {/*w  w  w. ja v a  2  s .  c  o m*/
        File file = new File(path);
        JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(object, file);

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Create a {@link Marshaller} for the given context and return it.
 * /* w  ww.j  a  va  2 s.  c o  m*/
 * @param jaxbContextName the context name where to look to find jaxb classes
 * @param cl : {@link ClassLoader} of the ObjectFactory
 * @return the created {@link Marshaller}
 * @throws JAXBException
 */
public static Marshaller createMarshaller(final String jaxbContextName, ClassLoader cl) throws JAXBException {
    final JAXBContext jaxbContext = JAXBContext.newInstance(jaxbContextName, cl);
    return jaxbContext.createMarshaller();
}

From source file:Main.java

public static <T> void saveObject(T object, Class<T> typeClass, URL path) {
    try {//  w  ww  .ja v a  2s.  c om
        File file = new File(path.toURI());
        JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(object, file);

    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String bean2Xml(Object bean) throws Exception {
    StringWriter writer = null;//www  .j  a v a2  s .  c  o  m
    try {
        JAXBContext jc = JAXBContext.newInstance(new Class[] { bean.getClass() });
        Marshaller m = jc.createMarshaller();

        m.setProperty("jaxb.fragment", Boolean.valueOf(true));
        writer = new StringWriter();
        m.marshal(bean, writer);
        return prefix + writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(writer);
    }
    return null;
}

From source file:Main.java

/**
 * genXml/* w  w w  .j  a v a2s .c o  m*/
 * 
 * @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();
    }
}