Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;

import javax.xml.bind.Unmarshaller;

import javax.xml.transform.stream.StreamSource;

public class Main {
    public static <T> T converyToJavaBean(String xmlStr, Class<T> c) {
        T t = null;
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> T converyToJavaBean(JAXBContext context, String xmlStr, Class<T> c) {
        JAXBElement<T> t = null;
        try {
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = (JAXBElement<T>) unmarshaller.unmarshal((new StreamSource(new StringReader(xmlStr))), c);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t.getValue();
    }
}