Example usage for javax.xml.bind Unmarshaller unmarshal

List of usage examples for javax.xml.bind Unmarshaller unmarshal

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller unmarshal.

Prototype

public Object unmarshal(javax.xml.stream.XMLEventReader reader) throws JAXBException;

Source Link

Document

Unmarshal XML data from the specified pull parser and return the resulting content tree.

Usage

From source file:Main.java

public static <T> T UnMarshall(Class<T> objectClass, XMLStreamReader reader) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(objectClass);
    Unmarshaller u = jc.createUnmarshaller();
    return objectClass.cast(u.unmarshal(reader));
}

From source file:Main.java

@SuppressWarnings({ "unchecked", "restriction" })
public static <T> T fromXML(String xml, Class<T> valueType) {
    try {//from  ww  w  .  j  ava2s . 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

@SuppressWarnings("unchecked")
public static <T> T unmarshall(String xml, Class<T> cl) throws JAXBException, UnsupportedEncodingException {

    JAXBContext jaxbCtx = JAXBContext.newInstance(cl);
    Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
    return (T) unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));

}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshall(InputStream xml, Class<T> cl) throws JAXBException {

    JAXBContext jaxbCtx = JAXBContext.newInstance(cl);
    Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
    return (T) unmarshaller.unmarshal(xml);

}

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

public static Object fromXml(@SuppressWarnings("rawtypes") Class clazz, String stringXml) {
    JAXBContext context = null;//from ww w.  j a va 2s  . c  om
    try {
        context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return unmarshaller.unmarshal(new StreamSource(new StringReader(stringXml)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
    T t = null;/*from w  w w .j  ava  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

/**
 * //  w w  w  .j  a  v  a  2 s . co  m
 * @param xmlData
 * @return
 * @return
 * @return
 * @throws JAXBException
 */
@SuppressWarnings("unchecked")
public static <T> T xmlStringToPojo(String xmlData, Class<T> targetClass) throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(targetClass);
    StringReader reader = new StringReader(xmlData);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return (T) unmarshaller.unmarshal(reader);
}

From source file:Main.java

public static Object convertToPojoUsingFile(String fileName, Class... type) {
    Object result;/*from   w w  w .j av a2s.c o  m*/
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        result = unmarshaller.unmarshal(new File(fileName));
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T ToJavaBean(String xml, Class<T> c) {
    T t = null;/*  w  w  w .jav a2s .  co 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;
}