Example usage for javax.xml.bind JAXBContext createUnmarshaller

List of usage examples for javax.xml.bind JAXBContext createUnmarshaller

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext createUnmarshaller.

Prototype

public abstract Unmarshaller createUnmarshaller() throws JAXBException;

Source Link

Document

Create an Unmarshaller object that can be used to convert XML data into a java content tree.

Usage

From source file:Main.java

public static <T> T converyToJavaBean(String xmlStr, Class<T> c) {
    T t = null;/*from  ww w . j  av  a  2 s . c o m*/
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return t;
}

From source file:Main.java

public static Object unmarshal(JAXBContext jaxbContext, XMLStreamReader xmlStreamReader) throws JAXBException {
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return unmarshaller.unmarshal(xmlStreamReader);
}

From source file:Main.java

public static <T> T xmlToObj(Class<T> t, String xml, String encoding) {
    try {//from ww w. java 2  s  . c o  m
        JAXBContext jaxbContext = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(encoding));
        @SuppressWarnings("unchecked")
        T obj = (T) unmarshaller.unmarshal(bais);
        bais.close();
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static <T> T unmarshall(InputStream fileStream, Class<?>... clazz) throws Exception {

    JAXBContext context = null;
    context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return (T) unmarshaller.unmarshal(fileStream);

}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> Object xmlToObject(String xmlContent, Class<T> clazz) throws JAXBException {

    ByteArrayInputStream xmlContentBytes = new ByteArrayInputStream(xmlContent.getBytes());
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setSchema(null); //note: setting schema to null will turn validator off
    Object unmarshalledObj = unmarshaller.unmarshal(xmlContentBytes);
    Object xmlObject = clazz.cast(unmarshalledObj);

    return (T) xmlObject;
}

From source file:Main.java

private static Unmarshaller getJaxbUnmarshaller(Class<?> classesToBeBound,
        Map<String, Object> unmarshallerProps) throws Exception {
    Unmarshaller unmarshaller = JAXB_UNMARSHALLER_CACHE.get(classesToBeBound);
    if (unmarshaller == null) {
        JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound);
        unmarshaller = jaxbContext.createUnmarshaller();
        if (unmarshallerProps != null && unmarshallerProps.size() > 0) {
            for (Map.Entry<String, Object> prop : unmarshallerProps.entrySet()) {
                unmarshaller.setProperty(prop.getKey(), prop.getValue());
            }//from  w  w  w . ja v a  2  s .  c o m
        }
        JAXB_UNMARSHALLER_CACHE.put(classesToBeBound, unmarshaller);
    }
    return unmarshaller;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshal(Class<T> clazz, InputStream input) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (T) jaxbUnmarshaller.unmarshal(input);
}

From source file:Main.java

public static Object unmarshal(String xml, Class<?> classObj) {
    Object obj;// w w  w . ja  v  a  2  s.  c o m
    try {
        JAXBContext jaxbContext = getJAXBContext(classObj);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        obj = unmarshaller.unmarshal(new StringReader(xml));
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlStr, Class<T> t) {
    try {/*  ww w  . j a  va2  s  .co  m*/
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(InputStream input, Class<T> t) {
    try {//from  ww  w .  ja va  2  s . com
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new InputStreamReader(input, "UTF-8"));
    } catch (JAXBException | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}