Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.Reader;

import java.io.UnsupportedEncodingException;

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

import org.xml.sax.InputSource;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T unmarshallXml(Class<T> clazz, InputStream in)
            throws JAXBException, UnsupportedEncodingException {

        String className = clazz.getPackage().getName();
        JAXBContext context = JAXBContext.newInstance(className);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        //Reader reader = new IgnoreIllegalCharactersXmlReader(in);

        Reader reader = new InputStreamReader(in, "UTF-8");

        InputSource is = new InputSource(reader);
        is.setEncoding("UTF-8");

        Object result = unmarshaller.unmarshal(is); //file);
        return (T) result;
    }
}