Java XML JAXB Unmarshaller unmarshalXML(String xmlString, Class classType)

Here you can find the source of unmarshalXML(String xmlString, Class classType)

Description

Un-marshall given string to given class type

License

Apache License

Parameter

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

Exception

Parameter Description
JAXBException an exception

Return

Instance of T

Declaration

public static <T> T unmarshalXML(String xmlString, Class<T> classType) throws JAXBException 

Method Source Code


//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;
    }
}

Related

  1. unmarshallString(String str, Class c)
  2. unmarshallXml(final String string, final Class type)
  3. unmarshallXMLtoObject(final byte[] xmlObject, final Class objectClass, final URL schemaURL)
  4. unmarshalObject(final InputStream input, final Class clazz)
  5. unmarshalPackage(InputStream pkgStream)
  6. unmarshalXmlToObject(String xmlFilePath, Class clazz)