Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.Reader;
import java.io.StringReader;

import java.io.Writer;
import javax.xml.bind.JAXBContext;

import javax.xml.bind.Unmarshaller;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T xml2Bean(String xml, Class<?> clazz) throws Exception {
        StringReader reader = null;
        try {
            JAXBContext jc = JAXBContext.newInstance(new Class[] { clazz });
            Unmarshaller m = jc.createUnmarshaller();
            reader = new StringReader(xml);
            return (T) m.unmarshal(reader);
        } catch (Exception e) {
        } finally {
            close(reader);
        }
        return null;
    }

    private static void close(Writer writer) {
        try {
            if (writer == null)
                return;
            writer.close();
        } catch (Exception localException) {
        }
    }

    private static void close(Reader reader) {
        try {
            if (reader == null)
                return;
            reader.close();
        } catch (Exception localException) {
        }
    }
}