Here you can find the source of unmarshalXML(String xmlString, Class
Parameter | Description |
---|---|
xmlString | XML String that is validated against its XSD |
classType | Root Class Name to convert XML String to Object |
T | Root Class that should return |
Parameter | Description |
---|---|
JAXBException | an exception |
public static <T> T unmarshalXML(String xmlString, Class<T> classType) throws JAXBException
//package com.java2s; //License from project: Apache License 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 v a 2 s. c om*/ * Un-marshall given string to given class type * * @param xmlString XML String that is validated against its XSD * @param classType Root Class Name to convert XML String to Object * @param <T> Root Class that should return * @return Instance of T * @throws JAXBException */ public static <T> T unmarshalXML(String xmlString, Class<T> classType) throws JAXBException { T t = null; if (xmlString != null) { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlString.toString().getBytes()); JAXBContext jaxbContext = JAXBContext.newInstance(classType); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); t = (T) jaxbUnmarshaller.unmarshal(byteArrayInputStream); } return t; } }