Java XML JAXB Unserialize inputStreamToObject(String xml, Class clazz, Class[] classes)

Here you can find the source of inputStreamToObject(String xml, Class clazz, Class[] classes)

Description

input Stream To Object

License

Apache License

Declaration

public static <T> T inputStreamToObject(String xml, Class<T> clazz, Class[] classes) 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;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class Main {
    public static <T> T inputStreamToObject(String xml, Class<T> clazz, Class[] classes) throws JAXBException {
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        JAXBContext jaxbContext = JAXBContext.newInstance(classes);
        //2. Use JAXBContext instance to create the Unmarshaller.
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        //3. Use the Unmarshaller to unmarshal the XML document to get an instance of JAXBElement.
        T unmarshal = (T) unmarshaller.unmarshal(stream);
        //    JAXBElement<T> unmarshalledObject = (JAXBElement<T>) unmarshaller.unmarshal(stream);
        //4. Get the instance of the required JAXB Root Class from the JAXBElement.
        //    T expenseObj = unmarshalledObject.getValue();

        return unmarshal;
    }/*ww  w .  j  a va  2  s.  c  om*/
}

Related

  1. inputStreamToObject(InputStream xml, Class clazz)
  2. load(Class clazz, InputStream is)
  3. load(Class clazz, InputStream is)
  4. load(File f, Class... args)
  5. load(final String xml, final Class clazz)