Java XML JAXB Marshaller xmlMarshal(Object obj, Class clazz)

Here you can find the source of xmlMarshal(Object obj, Class clazz)

Description

xml Marshal

License

Open Source License

Declaration

public static String xmlMarshal(Object obj, Class<?> clazz) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static String xmlMarshal(Object obj, Class<?> clazz) {
        StringWriter stringWriter = new StringWriter();

        try {//from   ww  w. ja  va 2s  .c  om
            JAXBContext context = JAXBContext.newInstance(clazz);
            Marshaller marshaller = context.createMarshaller();

            marshaller.marshal(obj, stringWriter);

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

        return stringWriter.toString();
    }
}

Related

  1. marshalToString(Object obj)
  2. marshalToString(Object obj)
  3. marshalV2(Class clazz, T obj, String uri, String nodeName)
  4. removeStandalone(final Marshaller marshaller)
  5. setEncoding(@Nonnull final Marshaller aMarshaller, @Nullable final Charset aEncoding)