Here you can find the source of unmarshal(String b, Class
Parameter | Description |
---|---|
b | The bean, represented as XML. |
implClass | The implementation class of the bean. |
bc | Additional classes to add to the JAXB context. |
public static <T> T unmarshal(String b, Class<T> implClass, Class<?>... bc)
//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 { /**/*from w w w.j a va2 s . co m*/ * 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); } } }