Java XML JAXB Object to XML toXmlString(T obj, Class type)

Here you can find the source of toXmlString(T obj, Class type)

Description

to Xml String

License

Open Source License

Declaration

public static <T> String toXmlString(T obj, Class<T> type) 

Method Source Code


//package com.java2s;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.ByteArrayOutputStream;

public class Main {
    public static <T> String toXmlString(T obj, Class<T> type) {
        try {/*ww w.ja  va  2s . c  o  m*/
            JAXBContext jaxbContext = JAXBContext.newInstance(type);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            jaxbContext.createMarshaller().marshal(obj, byteArrayOutputStream);
            return byteArrayOutputStream.toString();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. toXML(Object thing)
  2. toXml(T element)
  3. toXml(T object)
  4. toXMLString(final T binding)
  5. toXMLString(Object obj)
  6. write(Object jaxbAnnotatedObj, OutputStream os)
  7. write(String filepath, T content, Class typeParameterClass)
  8. writeJaxbObject(OutputStream outputStream, JAXBElement jaxbElement, Class modelClass)
  9. xmlEntityToString(final T entity)