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 String toXml(Object object) {
    final StringWriter out = new StringWriter();
    JAXBContext context = null;//from   www.j  a  v a 2 s. c  o  m
    try {
        context = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(object, new StreamResult(out));
    } catch (PropertyException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return out.toString();
}

From source file:Main.java

public static String obj2Xml(Object obj, String encoding) {
    String result = null;//from   w  w w. j av  a 2  s.  c  om
    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 final String obj2xml(final Object obj, final boolean formatted) throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext contextOut = JAXBContext.newInstance(obj.getClass());
    Marshaller marshallerOut = contextOut.createMarshaller();
    marshallerOut.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
    marshallerOut.marshal(obj, writer);/*from w  ww.  j a  v  a 2 s.  co m*/

    return new String(writer.getBuffer());
}

From source file:Main.java

public static String convertToXml(Object obj, String encoding) {
    String result = null;//w w w . ja v 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 <T extends Object> String marshalAsString(Class clz, T marshalObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clz);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter writer = new StringWriter();
    marshaller.marshal(marshalObj, new BufferedWriter(writer));
    return writer.toString();

}

From source file:Main.java

public static String saveObjectToXmlString(Object obj) throws JAXBException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    final StringWriter writer = new StringWriter();
    m.marshal(obj, writer);/*  www  . j a v a2  s  .co  m*/

    return writer.toString();
}

From source file:Main.java

public static String bean2Xml(Object bean) throws Exception {
    StringWriter writer = null;//from  w w  w.  j a va2  s.  c  o m
    try {
        JAXBContext jc = JAXBContext.newInstance(new Class[] { bean.getClass() });
        Marshaller m = jc.createMarshaller();

        m.setProperty("jaxb.fragment", Boolean.valueOf(true));
        writer = new StringWriter();
        m.marshal(bean, writer);
        return prefix + writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(writer);
    }
    return null;
}

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);
        }//from w w  w.  ja v a2 s . c o m

        //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 void saveInstance(File outputFile, Object instance) throws JAXBException, IOException {
    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(instance, outputFile);
}

From source file:Main.java

public static <T> void serialize(T object, OutputStream out) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(object, out);
}