Example usage for javax.xml.bind Marshaller marshal

List of usage examples for javax.xml.bind Marshaller marshal

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller marshal.

Prototype

public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;

Source Link

Document

Marshal the content tree rooted at jaxbElement into a javax.xml.stream.XMLEventWriter .

Usage

From source file:Main.java

public static <T> void xmlWriter(T entity, String xmlDestinationPath) {
    try {//from  w  w  w .j a  va 2  s . c om
        JAXBContext jc = JAXBContext.newInstance(entity.getClass());
        File destinationFile = new File(xmlDestinationPath);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(entity, destinationFile);
    } catch (JAXBException ex) {
    }
}

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   www  . ja  va 2 s .c  om*/
 * @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

/**
 * Stores object into file./*from  w w  w. jav a2  s  . c  om*/
 *
 * @param m marshaller
 * @param out output file
 * @param o object to be stored
 * @throws IOException if some error occurs
 */
private static void marshal(Marshaller m, File out, Object o) throws IOException {
    try (FileOutputStream fos = new FileOutputStream(out)) {
        m.marshal(o, fos);
    } catch (JAXBException ex) {
        throw new IOException("Cannot write object to file - " + ex.getMessage(), ex);
    }
}

From source file:Main.java

public static void pojoToXml(Object o, String arquivo) throws JAXBException {
    File file = new File(arquivo);
    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(o, file);
}

From source file:Main.java

/**
 * Serializes any Object to XML//w w w.  ja v a2s.  c om
 *
 * @param object Object to serialize
 * @return XML serialization of the supplied object
 */
public static String toXmlJaxb(Object object) {
    String result = "";
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        result = writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

/**
 * Helper method to serialize an object to XML. Requires object to be Serializable
 * @param entity Object to serialize//from  w  w w  .ja  v  a2  s. co m
 * @param <T> class that implements Serializable
 * @return String XML representation of object
 * @throws IOException if errors during serialization
 * @throws JAXBException if errors during serialization
 */
public static <T extends Serializable> String getXml(T entity) throws IOException, JAXBException {
    StringWriter sw = new StringWriter();

    if (null != entity) {
        JAXBContext ctx = JAXBContext.newInstance(entity.getClass());

        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        m.marshal(entity, sw);
    }
    sw.flush();
    sw.close();

    return sw.toString();
}

From source file:Main.java

public static <T extends Object> void marshal(Class clz, T marshalObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clz);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(marshalObj, System.out);

}

From source file:Main.java

/**
 *
 * @param obj//from   w w  w . j a va2 s .  c  o m
 * @throws JAXBException
 */
public static void serializeToSTD(Object obj) throws JAXBException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.marshal(obj, out);
}

From source file:Main.java

public final static void save(Object object, String path) {
    try {// www .j  av  a 2 s  .c  o m
        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

/**
 * Prints the document for debug.// w  w  w  .  ja  v  a2s . c o  m
 *
 * @param model the model to be printed
 * @throws Exception for any errors encountered
 */
public static void printModel(Object model) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance("gov.medicaid.domain.model");
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(model, System.out);
}