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 <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  w ww  . j a  v  a  2s .c o  m
    return writer.toString();
}

From source file:Main.java

public static String marshallToStringNoValidation(final Object xmlObject) throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(xmlObject.getClass());
    Marshaller jaxbMarshaller = context.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(xmlObject, writer);
    return writer.toString();
}

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 w  w  . j  a va2 s  . c  om*/
}

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 Document deserialize(Object object) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//  w  w  w  . java 2s  .co m
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(object, doc);
    return doc;
}

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

@SuppressWarnings("rawtypes")
public static String ObjToXml(Object object, boolean isXmlFormat, Class... classesToBeBound)
        throws JAXBException, IOException {
    JAXBContext context = JAXBContext.newInstance(classesToBeBound);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isXmlFormat);
    m.setSchema(null);/*  w ww. j a v  a 2 s .  c o m*/
    StringWriter sw = new StringWriter();
    m.marshal(object, sw);
    String result = sw.toString();
    sw.close();
    return result;
}

From source file:Main.java

public static OutputStream serializerOutputStream(Object xmlObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(xmlObj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(xmlObj, baos);// www. j  a v a  2 s. c  o m
    return baos;
}

From source file:Main.java

public static String marshal(Object object) throws Exception {
    StringWriter string = new StringWriter();
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
    marshaller.marshal(object, string);//from ww  w  .  j  a v a2  s.  c o  m
    return string.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  a  v a2s  .co  m

    return sw.toString();
}