Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

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

public class Main {
    public static <T> T xml2Obj(String xmlString, Class<T> clazz) throws RuntimeException {
        T c = null;
        Reader reader = null;
        try {
            reader = new StringReader(xmlString);
            Unmarshaller unMarshaller = JAXBContext.newInstance(clazz).createUnmarshaller();
            c = (T) unMarshaller.unmarshal(reader);
        } catch (JAXBException ex) {
            throw new RuntimeException(ex);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return c;
    }
}