Java XML JAXB Marshaller marshall(Object toMarshall)

Here you can find the source of marshall(Object toMarshall)

Description

Marshalls the object to a String.

License

Open Source License

Parameter

Parameter Description
toMarshall the object to Marshall

Exception

Parameter Description
JAXBException if there is an error marshalling the object

Return

the String representation

Declaration

public static final InputStream marshall(Object toMarshall) throws IOException 

Method Source Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;

import java.io.StringWriter;
import javax.xml.bind.JAXBContext;

import javax.xml.bind.Marshaller;
import com.amazonaws.util.StringInputStream;

public class Main {
    /**//from   w w  w  .j  a v  a  2s. c o  m
     * Marshalls the object to a String. <br/>
     * Example: <code>
     * String marshalled = JaxbUtil.marshall(env);
     * </code>
     * 
     * @param toMarshall
     *            the object to Marshall
     * @return the String representation
     * @throws JAXBException
     *             if there is an error marshalling the object
     */
    public static final InputStream marshall(Object toMarshall) throws IOException {
        StringWriter sw = null;
        try {
            JAXBContext ctx = JAXBContext.newInstance(toMarshall.getClass().getPackage().getName());
            sw = new StringWriter();
            Marshaller marshaller = ctx.createMarshaller();
            marshaller.setProperty("jaxb.formatted.output", true);
            marshaller.marshal(toMarshall, sw);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new StringInputStream(sw.toString());
    }
}

Related

  1. marshall(Class c, String xml)
  2. marshall(final Object o, Class clazz)
  3. marshall(Object o)
  4. marshall(Object obj)
  5. marshall(Object obj, URL schemaURL, Class... classesToBeBound)
  6. marshall(OutputStream os, JAXBElement element)
  7. marshall(String cntxtPkg, Object obj, OutputStream out)
  8. marshall(String file, JAXBElement object, Class context)
  9. marshaller(Object o, Class T)