Java tutorial
//package com.java2s; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; import java.io.UnsupportedEncodingException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class Main { @SuppressWarnings("unchecked") public static <T> T xmlToBean(String xmlStr, Class<T> t) { try { JAXBContext context = JAXBContext.newInstance(t); Unmarshaller unmarshaller = context.createUnmarshaller(); return (T) unmarshaller.unmarshal(new StringReader(xmlStr)); } catch (JAXBException e) { e.printStackTrace(); return null; } } @SuppressWarnings("unchecked") public static <T> T xmlToBean(InputStream input, Class<T> t) { try { JAXBContext context = JAXBContext.newInstance(t); Unmarshaller unmarshaller = context.createUnmarshaller(); return (T) unmarshaller.unmarshal(new InputStreamReader(input, "UTF-8")); } catch (JAXBException | UnsupportedEncodingException e) { e.printStackTrace(); } return null; } }