Java XML JAXB Unmarshaller unmarshal(String b, Class implClass, Class... bc)

Here you can find the source of unmarshal(String b, Class implClass, Class... bc)

Description

Convert XML data to a bean using JAXB.

License

Open Source License

Parameter

Parameter Description
b The bean, represented as XML.
implClass The implementation class of the bean.
bc Additional classes to add to the JAXB context.

Return

The bean, unmarshalled from the XML data using JAXB.

Declaration

public static <T> T unmarshal(String b, Class<T> implClass, Class<?>... bc) 

Method Source Code


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

Related

  1. unmarshal(InputStream stream, Class clazz)
  2. unmarshal(JAXBContext context, Class clazz, String source)
  3. unmarshal(JAXBContext jaxbContext, XMLStreamReader xmlStreamReader)
  4. unmarshal(org.w3c.dom.Element elem, Class c)
  5. unmarshal(org.w3c.dom.Element elem, Class c)
  6. unmarshal(String content, Class clasz)
  7. unMarshal(String contextPath, InputStream xmlStream)
  8. unmarshal(String ObjXml, Class configurationClass)
  9. unmarshal(String packageName, InputStream inputStream)