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

/**
 * @since 2.4/* ww  w . ja v  a2s.c o m*/
 */
public static Marshaller createMarshaller(final Object object) throws JAXBException {
    JAXBContext context = createContext(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    return marshaller;
}

From source file:Main.java

public static String toXxml(Object bean) {
    StringWriter stringWriter = null;
    try {/* w w  w  .j  ava2  s  . c o m*/
        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

/**
 * this method is responsible for converting any pojo to respective xml form
 * //w w  w .j a v  a  2 s  .  co  m
 * @param object
 * @param filePath
 * @throws JAXBException
 * @throws IOException
 */
public static File pojoToXml(Object object, String filePath) throws JAXBException, IOException {

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    OutputStream os = new FileOutputStream(filePath);
    marshaller.marshal(object, os);
    File file = new File(filePath);
    return file;
}

From source file:Main.java

/**
 * Object to XML/*from  w  ww.j av a2 s  .com*/
 * @param object
 * @return
 */
public static String convertToXML(Object object) {
    try {
        if (!mMap.containsKey(object.getClass())) {
            JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            mMap.put(object.getClass(), marshaller);
        }
        StringWriter stringWriter = new StringWriter();
        mMap.get(object.getClass()).marshal(object, stringWriter);
        return stringWriter.getBuffer().toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void marshal(Object obj, OutputStream out, Class... boundClasses) {
    try {/*w  ww. j  a va 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

/**
 * //from  w w w  .  ja va 2 s  . c  o  m
 * @param object
 * @return
 * @throws JAXBException
 */
public static String pojoToXml(Object object) throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // marshaller.setProperty(Marshaller.JAXB_ENCODING, "U");
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);
    String xmlData = writer.toString();
    return xmlData;
}

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  .co  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);
    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

/**
 * Generic method to Validate XML file while marshalling against their schema.
 * /* w  ww . ja  v a2s . com*/
 * @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

public static String toXml(Class className, Object object) {
    String strXml = "";
    StringWriter writer = null;/*from   w ww.  j a  v  a 2s  .  com*/
    try {
        writer = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(className);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, writer);
        strXml = writer.toString();
        writer.flush();

        strXml = strXml.replace("&lt;", "<");
        strXml = strXml.replace("&gt;", ">");
    } catch (Exception e) {
    } finally {
        if (writer != null) {
            try {
                writer.close();
                writer = null;
            } catch (Exception e) {
            }
        }
    }
    return strXml;
}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * /*  w ww .  j av a 2 s.  co m*/
 * @param bean
 *            The bean to marshal as XML.
 * @param bc
 *            Additional classes to register on the JAXB context for
 *            marshalling.
 * @return An XML representation of the bean.
 */
public static <T> String marshal(T bean, Class<?>... bc) {
    assert bean != null;
    Class<?>[] bind;
    if (bc.length > 0) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = bean.getClass();
        for (int i = 0; i < bc.length; i++)
            bind[i + 1] = bc[i];
    } else
        bind = new Class<?>[] { bean.getClass(), };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        marshaller.marshal(bean, baos);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error marshalling", e);
    }
    return new String(baos.toByteArray());
}