Java XML JAXB Object to XML asXml(T object)

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

Description

as Xml

License

Open Source License

Declaration

public static <T> String asXml(T object) 

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 asXml(T object) {
        try {//from   w  w w  .  j  a  v  a  2 s . co  m
            JAXBContext jaxbContext = null;
            jaxbContext = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            StringWriter writer = new StringWriter();
            marshaller.marshal(object, writer);
            return writer.toString();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. convertObjectToXML(Object object)
  2. convertObjectToXml(Object object)
  3. convertToString(Object obj)
  4. convertToXml(Object obj)