Java XML JAXB Object to XML convertToString(Object obj)

Here you can find the source of convertToString(Object obj)

Description

convert To String

License

Open Source License

Parameter

Parameter Description
args a parameter

Declaration

public static String convertToString(Object obj) 

Method Source Code

//package com.java2s;
/**//from www. j  a v a  2  s  .  c o m
 * =============================================================================
 *
 * ORCID (R) Open Source
 * http://orcid.org
 *
 * Copyright (c) 2012-2014 ORCID, Inc.
 * Licensed under an MIT-Style License (MIT)
 * http://orcid.org/open-source-license
 *
 * This copyright and license information (including a link to the full license)
 * shall be included in its entirety in all copies or substantial portion of
 * the software.
 *
 * =============================================================================
 */

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

public class Main {
    /**
     * @param args
     */
    public static String convertToString(Object obj) {
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            StringWriter writer = new StringWriter();
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(obj, writer);
            return writer.toString();
        } catch (JAXBException e) {
            return ("Unable to unmarshal because: " + e);
        }
    }
}

Related

  1. asXml(T object)
  2. convertObjectToXML(Object object)
  3. convertObjectToXml(Object object)
  4. convertToXml(Object obj)
  5. convertToXml(Object obj)
  6. convertToXml(Object obj, String encoding)
  7. createXML(Object o)