Java XML JAXB Object to XML toString(Object o, Class clazz)

Here you can find the source of toString(Object o, Class clazz)

Description

toString marshals given Jaxb object to a string (useful for debug)

License

Educational Community License

Parameter

Parameter Description
o jaxb object
clazz class of the jaxb object

Declaration

public static String toString(Object o, Class clazz) 

Method Source Code

//package com.java2s;
/**/*from   w  ww. j a v  a 2 s. c  o  m*/
 *  This document is a part of the source code and related artifacts
 *  for CollectionSpace, an open source collections management system
 *  for museums and related institutions.

 *  http://www.collectionspace.org
 *  http://wiki.collectionspace.org

 *  Copyright 2010 University of California at Berkeley

 *  Licensed under the Educational Community License (ECL), Version 2.0.
 *  You may not use this file except in compliance with this License.

 *  You may obtain a copy of the ECL 2.0 License at

 *  https://source.collectionspace.org/collection-space/LICENSE.txt

 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.StringWriter;

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

public class Main {
    /**
     * toString marshals given Jaxb object to a string (useful for debug)
     * @param o jaxb object
     * @param clazz class of the jaxb object
     * @return
     */
    public static String toString(Object o, Class clazz) {
        StringWriter sw = new StringWriter();
        try {
            JAXBContext jc = JAXBContext.newInstance(clazz);
            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(o, sw);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sw.toString();
    }
}

Related

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