Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import javax.xml.bind.Marshaller;

public class Main {
    /**
     * @param <T>
     *           the type to serialize
     * @param c
     *           the class of the type to serialize
     * @param o
     *           the instance containing the data to serialize
     * @return a string representation of the data.
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static <T> String marshal(Class<T> c, Object o) throws Exception {
        JAXBContext ctx = JAXBContext.newInstance(c);
        Marshaller marshaller = ctx.createMarshaller();
        StringWriter entityXml = new StringWriter();
        marshaller.marshal(o, entityXml);
        String entityString = entityXml.toString();
        return entityString;
    }
}