Java XML JAXB Object to XML toXML(Object thing)

Here you can find the source of toXML(Object thing)

Description

to XML

License

Open Source License

Declaration

public static String toXML(Object thing) 

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 toXML(Object thing) {
        StringWriter sw = new StringWriter();
        try {//from   w  w  w  .jav  a  2 s  .  c o  m
            JAXBContext jaxbContext = JAXBContext.newInstance(thing.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(thing, sw);
            return sw.toString();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. toXML(Object o)
  2. toXML(Object o, OutputStream os)
  3. toXml(Object o, OutputStream output)
  4. toXml(Object obj)
  5. toXml(Object object)
  6. toXml(T element)
  7. toXml(T object)
  8. toXMLString(final T binding)
  9. toXMLString(Object obj)