Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.concurrent.ConcurrentHashMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

public class Main {
    private static ConcurrentHashMap<String, JAXBContext> jaxbcontextmap = new ConcurrentHashMap<String, JAXBContext>();

    public static Object unmarshal(String xml, Class<?> classObj) {
        Object obj;
        try {
            JAXBContext jaxbContext = getJAXBContext(classObj);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            obj = unmarshaller.unmarshal(new StringReader(xml));
            return obj;
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JAXBContext getJAXBContext(Class<?> c) throws JAXBException {
        JAXBContext context = jaxbcontextmap.get(c.getName());
        if (context != null) {
            return context;
        } else {
            context = JAXBContext.newInstance(c);
            jaxbcontextmap.put(c.getName(), context);
            return context;
        }
    }
}