Java XML JAXB Object to XML convertObjectToXML(Object object)

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

Description

convert Object To XML

License

Open Source License

Declaration

public static String convertObjectToXML(Object 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 String convertObjectToXML(Object object) {
        StringWriter sw = new StringWriter();
        String xmlString = null;//from   w w w. ja v a2s .com
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.marshal(object, sw);
            xmlString = sw.toString();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        if (xmlString == null) {
            return "";
        } else {
            return xmlString;
        }
    }
}

Related

  1. asXml(T object)
  2. convertObjectToXml(Object object)
  3. convertToString(Object obj)
  4. convertToXml(Object obj)
  5. convertToXml(Object obj)
  6. convertToXml(Object obj, String encoding)