Java tutorial
//package com.java2s; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.ByteArrayInputStream; public class Main { /** * Convert XML data to a bean using JAXB. * * @param b * The bean, represented as XML. * @param implClass * The implementation class of the bean. * @param bc * Additional classes to add to the JAXB context. * @return The bean, unmarshalled from the XML data using JAXB. */ public static <T> T unmarshal(String b, Class<T> implClass, Class<?>... bc) { Class<?>[] bind; if (bc.length > 1) { bind = new Class<?>[bc.length + 1]; bind[0] = implClass; for (int i = 0; i < bc.length; i++) { bind[i + 1] = bc[i]; } } else { bind = new Class<?>[] { implClass, }; } ByteArrayInputStream bais = new ByteArrayInputStream(b.getBytes()); try { JAXBContext jaxbContext = JAXBContext.newInstance(bind); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return implClass.cast(unmarshaller.unmarshal(bais)); } catch (JAXBException e) { throw new IllegalStateException("Error unmarshalling", e); } } }