Java XML JAXB String to Object convertXMLToObject(byte[] data, Class clazz)

Here you can find the source of convertXMLToObject(byte[] data, Class clazz)

Description

convert XML To Object

License

Apache License

Declaration

public static <T> T convertXMLToObject(byte[] data, Class<T> clazz) throws JAXBException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import java.io.ByteArrayInputStream;

public class Main {
    public static <T> T convertXMLToObject(byte[] data, Class<T> clazz) throws JAXBException {

        javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(clazz.getPackage().getName());
        Unmarshaller um = jaxbCtx.createUnmarshaller();
        JAXBElement<T> element = (JAXBElement<T>) um.unmarshal(new ByteArrayInputStream(data));

        return element.getValue();
    }/*from ww w.j a v  a  2  s .co m*/
}

Related

  1. convertToJAXBException(String msg, Throwable e)
  2. convertToObject(Class clazz, InputStream inputStream)
  3. convertToXmlFile(File file, Object source, Class... type)
  4. convertXmlFileToObject(Class clazz, String xmlPath)
  5. convertXmlToObj(Class clazz, String xmlStr)
  6. converyToJavaBean(String xml, Class c)
  7. converyToJavaBean(String xml, Class c)
  8. converyToJavaBean(String xml, Class clazz)
  9. createObject(String xml, Object type)