Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

public class Main {
    /**
     * @param <T>
     *           the type we want to convert the XML into
     * @param c
     *           the class of the parameterized type
     * @param xml
     *           the instance XML description
     * @return a deserialization of the XML into an object of type T of class
     *         class <T>
     * @throws javax.xml.bind.JAXBException
     */
    @SuppressWarnings("unchecked")
    public static <T> T unmarshal(Class<T> c, String xml) throws JAXBException {
        T res;
        if (c == xml.getClass()) {
            res = (T) xml;
        } else {
            JAXBContext ctx = JAXBContext.newInstance(c);
            Unmarshaller marshaller = ctx.createUnmarshaller();
            res = (T) marshaller.unmarshal(new StringReader(xml));
        }
        return res;
    }
}