Java XML JAXB Object to XML convertToXml(Object obj, String encoding)

Here you can find the source of convertToXml(Object obj, String encoding)

Description

convert To Xml

License

Open Source License

Parameter

Parameter Description
obj a parameter
encoding a parameter

Declaration

public static String convertToXml(Object obj, String encoding) 

Method Source Code

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

import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Main {
    /**//from   w  w  w .  ja v a 2  s  .com
     *
     * @param obj
     * @param encoding 
     * @return 
     */
    public static String convertToXml(Object obj, String encoding) {
        String result = null;
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            //            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

            StringWriter writer = new StringWriter();
            marshaller.marshal(obj, writer);
            result = writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}

Related

  1. convertObjectToXML(Object object)
  2. convertObjectToXml(Object object)
  3. convertToString(Object obj)
  4. convertToXml(Object obj)
  5. convertToXml(Object obj)
  6. createXML(Object o)
  7. getObjects(List> objEls)
  8. getObjects(List objEls)
  9. getXmlString(JAXBElement versioningInfo, Boolean formatXml, Schema schema)