Example usage for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT

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

Introduction

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

Prototype

String JAXB_FORMATTED_OUTPUT

To view the source code for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT.

Click Source Link

Document

The name of the property used to specify whether or not the marshalled XML data is formatted with linefeeds and indentation.

Usage

From source file:Main.java

public final static void save(Object object, String path) {
    try {/*from  ww w  .j a  va 2  s  .  co 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

public static <T> void writeXml(Class<T> _class, T jaxbElement, OutputStream outputStream, String encoding)
        throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(_class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

    marshaller.marshal(jaxbElement, outputStream);
}

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

From source file:Main.java

public static <T> void xmlWriter(T entity, String xmlDestinationPath) {
    try {//from   w w w  . j  a  v a2s  .c o m
        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 String toString(Object entity, boolean formatOutput) {

    StringWriter stringWriter = new StringWriter();

    try {//w  w  w.j a  v  a2s  .  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

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  v  a  2s . co m*/
}

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  av  a 2  s. co  m*/

    return writer.toString();
}

From source file:Main.java

public static String toXxml(Object bean) {
    StringWriter stringWriter = null;
    try {//from   ww  w.j a  v  a  2 s .c  om
        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 convertToXml(Object obj, String encoding) {
    String result = null;//w  w  w  .  j  av  a 2 s. co  m
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static String bean2Xml(Object bean) {
    String xmlString = null;// w  w  w .j  a  v  a 2  s .co  m
    JAXBContext context;
    StringWriter writer;
    if (null == bean)
        return xmlString;
    try {
        context = JAXBContext.newInstance(bean.getClass());
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);//
        //m.setProperty(Marshaller.JAXB_ENCODING, "gb2312");//
        //m.setProperty(Marshaller.JAXB_ENCODING, "GBK");//
        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");//
        m.setProperty(Marshaller.JAXB_FRAGMENT, false);//
        writer = new StringWriter();
        m.marshal(bean, writer);
        xmlString = writer.toString();
        return xmlString;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return xmlString;
}