Java XML JAXB Object to XML toXml(T object)

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

Description

to Xml

License

Open Source License

Declaration

public static <T> String toXml(T object) throws JAXBException 

Method Source Code

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

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

import java.io.StringWriter;

public class Main {
    public static <T> String toXml(T object) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        return writer.toString();
    }/*from   w  w w .ja  va 2 s  . com*/
}

Related

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