Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T unmarshall(String xml, Class<T> cl) throws JAXBException, UnsupportedEncodingException {

        JAXBContext jaxbCtx = JAXBContext.newInstance(cl);
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));

    }

    @SuppressWarnings("unchecked")
    public static <T> T unmarshall(InputStream xml, Class<T> cl) throws JAXBException {

        JAXBContext jaxbCtx = JAXBContext.newInstance(cl);
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        return (T) unmarshaller.unmarshal(xml);

    }
}