Java XML JAXB Object to XML toXml(Class className, Object object)

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

Description

to Xml

License

Apache License

Declaration

public static String toXml(Class className, Object object) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

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

public class Main {

    public static String toXml(Class className, Object object) {
        String strXml = "";
        StringWriter writer = null;
        try {// w  ww. j  a  va  2s  .  c  om
            writer = new StringWriter();
            JAXBContext context = JAXBContext.newInstance(className);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(object, writer);
            strXml = writer.toString();
            writer.flush();

            strXml = strXml.replace("&lt;", "<");
            strXml = strXml.replace("&gt;", ">");
        } catch (Exception e) {
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                    writer = null;
                } catch (Exception e) {
                }
            }
        }
        return strXml;
    }
}

Related

  1. objectToXML(Class cls, Object entity)
  2. ObjectToXml(Object object)
  3. objectToXml(Object source, Class... type)
  4. toString(E e)
  5. toString(Object o, Class clazz)
  6. toXML(final Object obj, final boolean prettyPrint)
  7. toXml(final T dto)
  8. toXML(Object o)
  9. toXML(Object o, OutputStream os)