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 Object xmlToPojo(Object o, String arquivo) throws JAXBException {
    File file = new File(arquivo);

    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    return jaxbUnmarshaller.unmarshal(file);

}

From source file:Main.java

public static <T> T fromXML(String xml, Class<T> valueType) {
    try {/*from w  ww. ja  va2s  .co  m*/
        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> Object fromXml(String xmlStr, Class<T> pojoClass) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(pojoClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object object = unmarshaller.unmarshal(new ByteArrayInputStream(xmlStr.getBytes()));
    return object;
}

From source file:Main.java

public static <T> T deserializeJaxb(Class<T> cls, Reader reader) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(cls);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return (T) unmarshaller.unmarshal(reader);
}

From source file:Main.java

public static <T> T deserializeJaxb(Class<T> cls, Node node) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(cls);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return (T) unmarshaller.unmarshal(node);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T fromXML(String xml, Class<T> valueType) {
    try {/*w w w  . j a  v  a2 s  . com*/
        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> T unmarshal(JAXBContext context, Class<T> clazz, String source) throws JAXBException {

    if (source == null) {
        return null;
    }/*from   ww w  .  ja v a  2s  . c o  m*/

    StringReader reader = new StringReader(source);
    if (context == null) {
        context = JAXBContext.newInstance(clazz);
    }
    Unmarshaller un = context.createUnmarshaller();
    return (T) un.unmarshal(reader);
}

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 a2  s . com*/
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        System.out.println(e);
    }
    return t;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToObject(String str, Class<T> clszz) {
    JAXBContext context = null;//from  w  w  w  .  j a  va 2s .com
    T obj = null;

    try {
        context = JAXBContext.newInstance(clszz);
        StringReader reader = new StringReader(str);
        Unmarshaller unmar = context.createUnmarshaller();
        obj = (T) unmar.unmarshal(reader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

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