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.IOException;

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

public class Main {

    public static <T> T xmlToObj(Class<T> t, String xml, String encoding) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(t);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(encoding));
            @SuppressWarnings("unchecked")
            T obj = (T) unmarshaller.unmarshal(bais);
            bais.close();
            return obj;
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}