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:eu.hydrologis.jgrass.featureeditor.utils.Utilities.java

/**
 * Write the {@link AForm} to xml file./* www  . ja  va  2 s. c  om*/
 * 
 * @param form the {@link AForm} object.
 * @param file the file to which to dump to. 
 * @throws Exception
 */
public static void writeXML(AForm form, File file) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(AForm.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(form, file);
}

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. j ava 2  s  .co  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

public static String parseObjToXmlString(Object obj) {
    if (obj == null) {
        return noResult;
    }/* ww w .  j a va 2 s .co 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

public static String parseObject2XmlString(Object object) {
    if (object == null) {
        return noResult;
    }//from  w  ww  .j a va2s.c  o  m
    StringWriter sw = new StringWriter();
    JAXBContext jAXBContent;
    Marshaller marshaller;
    try {
        jAXBContent = JAXBContext.newInstance(object.getClass());
        marshaller = jAXBContent.createMarshaller();
        marshaller.marshal(object, sw);
        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return noResult;
    }
}

From source file:Main.java

private static String parseObject2XmlString(Object object) {
    if (object == null) {
        return noResult;
    }/*  w w w .j  ava 2  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;
    }
}

From source file:Main.java

public static <T> String write(T content, Class<T> typeParameterClass) {
    ByteArrayOutputStream baos = null;
    try {//from   www. ja  va  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:com.redhat.akashche.wixgen.cli.Launcher.java

private static Marshaller createMarshaller() throws Exception {
    JAXBContext jaxb = JAXBContext.newInstance(Wix.class.getPackage().getName());
    Marshaller marshaller = jaxb.createMarshaller();
    marshaller.setProperty(JAXB_FORMATTED_OUTPUT, true);
    return marshaller;
}

From source file:Main.java

public static String marshaller(Object o, Class<?> T) {
    JAXBContext jc;
    Marshaller marshaller;//from  www  . jav a 2  s . com
    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:gov.hhs.fha.nhinc.lift.proxy.util.ProxyUtil.java

public static String marshalToString(Object obj) throws JAXBException {
    StringWriter writer = new StringWriter();

    JAXBContext jc = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = jc.createMarshaller();

    marshaller.marshal(obj, writer);/*w w  w .  jav a  2  s  . co  m*/
    log.info("Marshal: " + writer.toString());

    return writer.toString();
}

From source file:eu.planets_project.tb.impl.serialization.ExperimentRecords.java

/**
 * @param exp/*from  www  .  j  av  a2s.co  m*/
 * @param out
 */
private static void writeToOutputStream(ExperimentRecords exp, OutputStream out) {
    try {
        JAXBContext jc = JAXBContext.newInstance(ExperimentRecords.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(exp, out);
    } catch (JAXBException e) {
        log.fatal("Writing Experiments to XML failed: " + e);
    }
}