Example usage for javax.xml.bind Marshaller setProperty

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

Introduction

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

Prototype

public void setProperty(String name, Object value) throws PropertyException;

Source Link

Document

Set the particular property in the underlying implementation of Marshaller .

Usage

From source file:Main.java

public static void writeXML(Class<?> class1, Object obj, File outputFile)
        throws JAXBException, FileNotFoundException {
    JAXBContext context = JAXBContext.newInstance(class1);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(obj, outputFile);//from w ww  .  j  av a  2s  .c  om
}

From source file:Main.java

public static void parseBeanToXmlFileByJAXB(String path, Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);
    marshaller.marshal(rootElement, new FileOutputStream(path));
}

From source file:Main.java

public static String objectToXML(Class clazz, Object object) throws JAXBException {
    String xml = null;//from w  ww  .j ava  2s .  c  o  m
    JAXBContext context = JAXBContext.newInstance(clazz);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    Writer w = new StringWriter();
    m.marshal(object, w);
    xml = w.toString();
    return xml;
}

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);/* w  ww  . j  a  va  2s. c  o m*/
}

From source file:Main.java

public static <T> String toString(T xml) {
    JAXBContext jc;/* ww  w  . j ava 2 s .  c  om*/
    try {
        jc = JAXBContext.newInstance(xml.getClass());
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter writer = new StringWriter();
        m.marshal(xml, writer);
        return writer.toString();
    } catch (JAXBException e) {
        return "";
    }
}

From source file:Main.java

public static Marshaller getMarshaller(Class jaxbClass) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(jaxbClass);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    return marshaller;
}

From source file:Main.java

public static <T> void JAXBMarshalling(T object, File output) throws JAXBException {
    JAXBContext jAXBContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jAXBContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.marshal(object, output);//from ww  w .j av  a  2  s.c o m
}

From source file:Main.java

public static <T> void xmlWriter(T entity, String xmlDestinationPath) {
    try {//from   w  ww.j  av a2 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

public static <T> String print(T xml, 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);

    StringWriter writer = new StringWriter();
    marshaller.marshal(xml, writer);/*from w w  w  .j  a  va 2  s .  c o m*/

    return writer.toString();
}

From source file:Main.java

public static String object2Xml(Object obj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    StringWriter writer = new StringWriter();
    marshaller.marshal(new JAXBElement(new QName("xml"), obj.getClass(), obj), writer);

    return writer.toString();
}