Java XML JAXB Marshaller marshal(Class clazz, T object)

Here you can find the source of marshal(Class clazz, T object)

Description

marshal

License

Open Source License

Declaration

public static <T> String marshal(Class<T> clazz, T object) throws JAXBException, PropertyException 

Method Source Code

//package com.java2s;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import javax.xml.namespace.QName;

public class Main {
    public static <T> String marshal(Class<T> clazz, T object) throws JAXBException, PropertyException {
        StringWriter writer = new StringWriter();
        JAXBContext jc = JAXBContext.newInstance(clazz);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(new JAXBElement<T>(new QName("body"), clazz, object), writer);
        String actual = writer.toString();
        return actual;
    }/*  w  w w .ja va 2s. c o m*/
}

Related

  1. isSunJAXB2Marshaller(@Nullable final Marshaller aMarshaller)
  2. marshal(Class clz, T marshalObj)
  3. marshal(Class c, String xml)
  4. marshal(Class c, String xml)
  5. marshal(Class clazz, T obj)
  6. marshal(final Object obj, final Class... classes)
  7. marshal(final Object object)
  8. marshal(final Object object)
  9. marshal(JAXBContext context, Object object, Writer writer, Map properties)