Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

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

import javax.xml.bind.Unmarshaller;

public class Main {

    @SuppressWarnings("unchecked")
    public static <T> T unserializer(Class<T> clazz, String xml) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        T obj = (T) unmarshaller.unmarshal(new StringReader(xml));
        return obj;
    }

    public static <T> T unserializer(Class<T> clazz, byte[] xml) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        InputStream is = new ByteArrayInputStream(xml);
        @SuppressWarnings("unchecked")
        T obj = (T) unmarshaller.unmarshal(is);
        return obj;
    }

    public static <T> T unserializer(Class<T> clazz, InputStream xmlInput) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        @SuppressWarnings("unchecked")
        T obj = (T) unmarshaller.unmarshal(xmlInput);
        return obj;
    }
}