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> Object JAXBUnmarshalling(Class insClass, String fileSource) throws JAXBException {
    JAXBContext jAXBContext = JAXBContext.newInstance(insClass);
    Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();

    File file = new File(fileSource);
    Object object = unmarshaller.unmarshal(file);
    return object;
}

From source file:Main.java

public static <T> T unMarshal(String xml, Class<T> clazz, String encoding) {
    T result = null;//from  w ww.java 2  s .  c o  m
    try {
        ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
        JAXBContext context = JAXBContext.newInstance(clazz);
        result = (T) context.createUnmarshaller()
                .unmarshal(new InputSource(new InputStreamReader(is, encoding)));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return result;

}

From source file:Main.java

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

    return t;
}

From source file:Main.java

public static Object xmlStrToObj(String inputStr, Class inputClass) throws Exception {
    byte[] byteArray = inputStr.getBytes();
    ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);
    XMLInputFactory input = XMLInputFactory.newFactory();
    XMLStreamReader reader = input.createXMLStreamReader(byteStream);

    JAXBContext context = JAXBContext.newInstance(inputClass);
    Unmarshaller unmarsh = context.createUnmarshaller();
    Object result = unmarsh.unmarshal(reader);

    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T ToJavaBean(String xml, Class<T> c) {
    T t = null;//from  ww  w . ja v  a2  s .  c  o  m
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return t;
}

From source file:Main.java

public static <T> T fromXML(String xml, Class<T> valueType) {
    try {//w  ww.  jav a2  s .  c  om
        JAXBContext context = JAXBContext.newInstance(valueType);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

public static <T extends Object> T unmarshalFromString(Class clz, String input) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    T unobj = (T) unmarshaller.unmarshal(new StreamSource(new StringReader(input.toString())));
    return unobj;
}

From source file:Main.java

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

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T fromXML(String xml, Class<T> valueType) {
    try {/*w ww  .j a v a  2  s .  c om*/
        JAXBContext context = JAXBContext.newInstance(valueType);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

/**
 * Unmarshal.//from   ww w .j  a v  a 2 s  .  c  o  m
 *
 * @param inputSource the input source
 * @param clazz the clazz
 * @return the object
 * @throws JAXBException the jAXB exception
 */
public static Object unmarshal(InputSource inputSource, Class<?> clazz) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller u = jc.createUnmarshaller();
    ValidationEventCollector vec = new ValidationEventCollector();
    u.setEventHandler(vec);
    Object o1 = u.unmarshal(inputSource);
    return o1;

}