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 getXmlString(JAXBElement versioningInfo, Boolean formatXml, Schema schema)
        throws JAXBException {
    String packageName = versioningInfo.getValue().getClass().getPackage().getName();
    JAXBContext context = JAXBContext.newInstance(packageName);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatXml);

    if (schema != null) {
        marshaller.setSchema(schema);//from w  w w.j a v a 2 s .  co  m
    }

    ByteArrayOutputStream oStream = new ByteArrayOutputStream();
    marshaller.marshal(versioningInfo, oStream);

    return oStream.toString();
}

From source file:Main.java

private static Marshaller createMarshaller(Object o) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    return jaxbMarshaller;
}

From source file:Main.java

public static String convertToXmlString(Object source) {
    try {/*from ww w. j a  v  a  2s . co m*/
        StringWriter sw = new StringWriter();
        JAXBContext jAXBContext = JAXBContext.newInstance(source.getClass());
        Marshaller marshaller = jAXBContext.createMarshaller();
        marshaller.marshal(source, sw);
        return sw.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static OutputStream serializerOutputStream(Object xmlObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(xmlObj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(xmlObj, baos);//from  w w  w .jav a  2 s  . c  om
    return baos;
}

From source file:Main.java

public static <T> void save(File file, T obj, Class<?>... clazz) throws JAXBException {
    //Create JAXB Context
    JAXBContext jc = JAXBContext.newInstance(clazz);
    //Create marshaller
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    marshaller.marshal(obj, file);/*from   ww  w  . j a  va  2 s . co  m*/
}

From source file:Main.java

public static String marshal(Object object) throws Exception {
    StringWriter string = new StringWriter();
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
    marshaller.marshal(object, string);/*www  . j ava 2  s.  c  o  m*/
    return string.toString();
}

From source file:Main.java

/**
 * Saves the data in the file in xml format.
 *
 * @param file Points to a valid xml file containing data that match the {@code classToConvert}.
 *             Cannot be null.//from  w  ww. ja v  a  2 s .co  m
 * @throws FileNotFoundException Thrown if the file is missing.
 * @throws JAXBException         Thrown if there is an error during converting the data
 *                               into xml and writing to the file.
 */
public static <T> void saveDataToFile(File file, T data) throws FileNotFoundException, JAXBException {

    assert file != null;
    assert data != null;

    if (!file.exists()) {
        throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
    }

    JAXBContext context = JAXBContext.newInstance(data.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    m.marshal(data, file);
}

From source file:Main.java

public static <T> String generateObject2XMLString(T object) throws JAXBException {

    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(object, sw);/* w  w  w . j  a  v a  2 s. c om*/

    return sw.toString();
}

From source file:Main.java

public final static void save(Object object, String path) {
    try {/*from   www .  j  av a2s . c om*/
        File file = new File(path);
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(object, file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String parseObjectToXml(Object obj) {
    if (obj == null) {
        return NO_RESULT;
    }/*  w  w  w  .  j a v a 2 s.c o 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;
}