Java XML JAXB Marshaller marshalAsString(Class clz, T marshalObj)

Here you can find the source of marshalAsString(Class clz, T marshalObj)

Description

marshal As String

License

Open Source License

Declaration

public static <T extends Object> String marshalAsString(Class clz, T marshalObj) throws JAXBException 

Method Source Code


//package com.java2s;
/* ?Copyright 2012 Megam Systems??
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version./*from  w  w w.  j a va  2 s  .  c o  m*/
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.BufferedWriter;

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

public class Main {
    public static <T extends Object> String marshalAsString(Class clz, T marshalObj) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(clz);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter writer = new StringWriter();
        marshaller.marshal(marshalObj, new BufferedWriter(writer));
        return writer.toString();

    }

    public static <T extends Object> void marshal(Class clz, T marshalObj) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(clz);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(marshalObj, System.out);

    }
}

Related

  1. marshal(Object source, Class configurationClass)
  2. marshal(T bean, Class... bc)
  3. marshal(T clazz)
  4. marshal(T object)
  5. marshal(T t, Class entityClass)
  6. marshall(Class c, String xml)
  7. marshall(final Object o, Class clazz)
  8. marshall(Object o)
  9. marshall(Object obj)