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.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import org.xml.sax.InputSource;

public class Main {
    public static <T> T unMarshal(String xml, Class<T> clazz) {
        return unMarshal(xml, clazz, "GBK");
    }

    public static <T> T unMarshal(String xml, Class<T> clazz, String encoding) {
        T result = null;
        try {
            ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
            JAXBContext context = JAXBContext.newInstance(clazz);
            result = (T) context.createUnmarshaller()
                    .unmarshal(new InputSource(new InputStreamReader(is, encoding)));
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;

    }
}