Java XML JAXB Object to XML toXml(Object object)

Here you can find the source of toXml(Object object)

Description

to Xml

License

Open Source License

Declaration

public static String toXml(Object object) 

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;
import javax.xml.bind.PropertyException;

import javax.xml.transform.stream.StreamResult;

public class Main {
    public static String toXml(Object object) {
        final StringWriter out = new StringWriter();
        JAXBContext context = null;
        try {//from  w w  w . j  a  v  a  2s  .c om
            context = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(object, new StreamResult(out));
        } catch (PropertyException e) {
            e.printStackTrace();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return out.toString();
    }
}

Related

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