Here you can find the source of toObject(String xml, Class
@SuppressWarnings("unchecked") public static <T> T toObject(String xml, Class<T> c)
//package com.java2s; //License from project: Apache License import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.transform.stream.StreamSource; public class Main { @SuppressWarnings("unchecked") public static <T> T toObject(String xml, Class<T> c) { if (xml == null || "".equals(xml.trim())) { return null; }//from w w w . j ava 2s.co m StringReader reader = null; try { JAXBContext context = JAXBContext.newInstance(c); javax.xml.bind.Unmarshaller unmarshaller = context.createUnmarshaller(); reader = new StringReader(xml); T result = (T) unmarshaller.unmarshal(new StreamSource(reader)); return result; } catch (Exception e) { e.printStackTrace(); ; throw new RuntimeException(e.getMessage(), e); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } } } }