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

@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);//ww  w  . j av  a2 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

/**
 * Generic method to Validate XML file while marshalling against their schema.
 * //from   w  w  w .j a v a  2s  .c  om
 * @param context
 * @param schemaFile
 * @param object
 * @return
 * @throws SAXException
 * @throws JAXBException
 */
public static String validateAndMarshallXML(JAXBContext context, String schemaFile, Object object)
        throws SAXException, JAXBException {
    String xmlFormOfBean = null;

    //      if (context != null && (schemaFile != null && schemaFile.trim().length() > 0)) {
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    QName qname = new QName("www.genpact.com", "CobCmData");
    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "www.genpact.com cob_cm.xsd ");
    //         SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);   // thread- safe 
    //         marshaller.setSchema(sf.newSchema(new File(schemaFile)));   // validate jaxb context against schema 
    ByteArrayOutputStream sos = new ByteArrayOutputStream(); // for XML output into string

    //         JAXBElement<CobCmData> rootElement = new JAXBElement<CobCmData>(qname,CobCmData.class,(CobCmData)object);
    //         marshaller.marshal(rootElement, sos);  
    //         xmlFormOfBean = sos.toString();

    //      }
    return xmlFormOfBean;
}

From source file:Main.java

public static <T> void marshal(Object jaxbElement, Class<T> jaxbFactory, OutputStream out)
        throws JAXBException {
    if (!(jaxbElement instanceof JAXBElement<?>)) {
        throw new JAXBException("Must be a instance of JAXBElement<?>");
    }/*from w  w w. j a  v  a2  s.  c  o  m*/
    try {
        JAXBContext jc = JAXBContext.newInstance(jaxbFactory);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(jaxbElement, out);

    } catch (PropertyException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void marshal(Object model, OutputStream output) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(model.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    jaxbMarshaller.marshal(model, output);
}

From source file:Main.java

public static void marshal(Object obj, OutputStream out, Class... boundClasses) {
    try {//from  www .j  a v  a  2 s .  c o m
        Marshaller m = JAXBContext.newInstance(boundClasses).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(obj, out);
    } catch (Exception ex) {
    }
}

From source file:Main.java

/**
 * Generic method to Validate XML file while marshalling against their schema.
 * /*ww w  . jav  a  2  s.c  o m*/
 * @param context
 * @param schemaFile
 * @param object
 * @return
 * @throws SAXException
 * @throws JAXBException
 */
public static String validateAndMarshallXML(JAXBContext context, String schemaFile, Object object)
        throws SAXException, JAXBException {
    String xmlFormOfBean = null;

    if (context != null && (schemaFile != null && schemaFile.trim().length() > 0)) {
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // thread- safe 
        marshaller.setSchema(sf.newSchema(new File(schemaFile))); // validate jaxb context against schema 
        ByteArrayOutputStream sos = new ByteArrayOutputStream(); // for XML output into string

        marshaller.marshal(object, sos);
        xmlFormOfBean = sos.toString();

    }
    return xmlFormOfBean;
}

From source file:Main.java

/**
 * //from   w ww  .jav  a 2  s .co  m
 * @param contextPath
 * @return Marshaller
 * @throws JAXBException
 */
public static Marshaller getMarshaller(final String contextPath) throws JAXBException {
    final JAXBContext jaxbContext = JAXBContext.newInstance(contextPath);
    final Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    return marshaller;
}

From source file:Main.java

/**
 * Helper method to serialize an object to XML. Requires object to be Serializable
 * @param entity Object to serialize/*from www.  j a  v a 2 s  .c  o  m*/
 * @param <T> class that implements Serializable
 * @return String XML representation of object
 * @throws IOException if errors during serialization
 * @throws JAXBException if errors during serialization
 */
public static <T extends Serializable> String getXml(T entity) throws IOException, JAXBException {
    StringWriter sw = new StringWriter();

    if (null != entity) {
        JAXBContext ctx = JAXBContext.newInstance(entity.getClass());

        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        m.marshal(entity, sw);
    }
    sw.flush();
    sw.close();

    return sw.toString();
}

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

public static void saveInstance(Writer output, Object instance)

        throws JAXBException, IOException {

    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING);

    marshaller.marshal(instance, output);

    output.flush();//from ww  w.  jav a  2s .  c o m

}