Java tutorial
//package com.java2s; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.StringReader; public class Main { /** * Marshal a object of * <code>classItem</code> from the xmlResponse * <code>String</code>. * * @param xmlResponse <code>String</code> that represents the object to be * marshal. * @param classItem <code>class</code> of the returns object. * @return a object of <code>classItem</code>. * @throws JAXBException throw trying to marshal. */ public static Object unmarshalFromString(String xmlResponse, Class classItem) throws JAXBException { final JAXBContext jaxbContext = JAXBContext.newInstance(classItem); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = null; try { reader = new StringReader(xmlResponse); return jaxbUnmarshaller.unmarshal(reader); } finally { if (reader != null) reader.close(); } } /** * Marshal a object of * <code>classItem</code> from the xmlResponse * <code>String</code>. * * @param xmlResponse <code>String</code> that represents the object to be * marshal. * @param classItem <code>String</code> of the returns object. * @return a object of <code>classItem</code>. * @throws JAXBException throw trying to marshal. */ public static Object unmarshalFromString(String xmlResponse, String classItem) throws JAXBException { final JAXBContext jaxbContext = JAXBContext.newInstance(classItem); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = null; try { reader = new StringReader(xmlResponse); return jaxbUnmarshaller.unmarshal(reader); } finally { if (reader != null) reader.close(); } } }