Java XML JAXB Object to XML objectToXml(Object source, Class... type)

Here you can find the source of objectToXml(Object source, Class... type)

Description

object To Xml

License

Apache License

Declaration

@SuppressWarnings("rawtypes")
    public static String objectToXml(Object source, Class... type) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    @SuppressWarnings("rawtypes")
    public static String objectToXml(Object source, Class... type) {
        String result;/* w  w  w  .j  av a  2s .  c o  m*/
        StringWriter sw = new StringWriter();
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(type);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(source, sw);
            result = sw.toString();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }

        return result;
    }
}

Related

  1. getXMLString(Object person, Class clazz)
  2. getXmlString(T jaxbObject)
  3. Object2Xml(Object object)
  4. objectToXML(Class cls, Object entity)
  5. ObjectToXml(Object object)
  6. toString(E e)
  7. toString(Object o, Class clazz)
  8. toXml(Class className, Object object)
  9. toXML(final Object obj, final boolean prettyPrint)