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 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 <T> String marshal(T object) throws JAXBException {
    final Marshaller marshaller = JAXBContext.newInstance(object.getClass()).createMarshaller();
    final StringWriter stringWriter = new StringWriter();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(object, stringWriter);

    return stringWriter.toString();
}

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);//from w w w .j av a 2  s . co  m

    return sw.toString();
}

From source file:Main.java

public static File Marshall(Object object, String filePath) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(object.getClass());
    Marshaller u = jc.createMarshaller();
    u.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    File f = new File(filePath);
    u.marshal(object, f);//from  ww w.jav a  2  s .com
    return f;
}

From source file:Main.java

public static String objectToXML(Class clazz, Object object) throws JAXBException {
    String xml = null;/*from  w ww .  j  av  a  2s. co  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 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 w  ww. j  a  v a2 s.c  o m
}

From source file:Main.java

public static <T> String toXml(T object) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);/*from www . j a v  a2 s .com*/
    return writer.toString();
}

From source file:Main.java

public static String parseBeanToXmlStringByJAXB(Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);

    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        public void write(int b) throws IOException {
            this.string.append((char) b);
        }// w  ww .  j  ava 2s  .c  om

        //Netbeans IDE automatically overrides this toString()
        public String toString() {
            return this.string.toString();
        }
    };

    marshaller.marshal(rootElement, output);

    return output.toString();

}

From source file:Main.java

public static String toXML(Object obj) {
    try {/*from  ww w  . ja va2 s .  co  m*/
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
        return "";
    }
}

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  w  w  .  j av a2s  .co m
}