Java XML JAXB Object to XML XmlObjectToString(Object o)

Here you can find the source of XmlObjectToString(Object o)

Description

Xml Object To String

License

Open Source License

Declaration

public static String XmlObjectToString(Object o) 

Method Source Code

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

import java.io.ByteArrayOutputStream;

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

public class Main {
    public static String XmlObjectToString(Object o) {
        OutputStream baos = new ByteArrayOutputStream();
        try {//from  w  w  w.  ja  v a  2s .c o m
            JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(o, baos);
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return baos.toString();

    }
}

Related

  1. toXmlString(T obj, Class type)
  2. write(Object jaxbAnnotatedObj, OutputStream os)
  3. write(String filepath, T content, Class typeParameterClass)
  4. writeJaxbObject(OutputStream outputStream, JAXBElement jaxbElement, Class modelClass)
  5. xmlEntityToString(final T entity)