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

@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
    T t = null;/*from w  w  w  . ja v a2s.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 responseBody, Class<T> c) throws JAXBException, XMLStreamException {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(responseBody.getBytes());
    JAXBContext jaxbContext = JAXBContext.newInstance(c);
    XMLStreamReader xsr = xmlInputFactory.createXMLStreamReader(inputStream);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (T) jaxbUnmarshaller.unmarshal(xsr);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xml2obj(String xml, Class<T> type) {
    T obj = null;//www  .j  a v  a 2s .  c  o m
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        obj = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

public static final Object xml2obj(final Class<?> cls, final String xml) throws JAXBException {
    StringReader reader = new StringReader(xml);
    JAXBContext contextIn = JAXBContext.newInstance(cls);
    Unmarshaller marshallerIn = contextIn.createUnmarshaller();

    return marshallerIn.unmarshal(reader);
}

From source file:Main.java

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

From source file:Main.java

public final static Object load(Class clazz, String name) {
    try {//from  ww  w  . ja  v a 2s . c o m
        File file = new File(name);
        JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return jaxbUnmarshaller.unmarshal(file);
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Object xml2BeanUtf8(Class<?> zClass, String xml) {
    Object obj = null;/*from   ww w.  j  av  a  2s.c  om*/
    JAXBContext context = null;
    if (null == xml || "".equals(xml) || "null".equalsIgnoreCase(xml) || xml.length() < 1)
        return obj;
    try {
        context = JAXBContext.newInstance(zClass);
        InputStream iStream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        Unmarshaller um = context.createUnmarshaller();
        obj = (Object) um.unmarshal(iStream);
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return obj;
}

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

public static Object xml2Bean(Class<?> zClass, String xml) {
    Object obj = null;//w w  w. j a va 2s  . c o m
    JAXBContext context = null;
    if (null == xml || "".equals(xml) || "null".equalsIgnoreCase(xml) || xml.length() < 1)
        return obj;
    try {
        context = JAXBContext.newInstance(zClass);
        InputStream iStream = new ByteArrayInputStream(xml.getBytes());
        Unmarshaller um = context.createUnmarshaller();
        obj = (Object) um.unmarshal(iStream);
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unserializer(Class<T> clazz, String xml) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    T obj = (T) unmarshaller.unmarshal(new StringReader(xml));
    return obj;/*from w w w  .j  a  va2  s.  c o  m*/
}