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

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

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T parseXml(File file, Class<T> clazz) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller shaller = context.createUnmarshaller();
    return (T) shaller.unmarshal(file);
}

From source file:Main.java

public static <T> T xml2Object(String xml, Class<T> clazz) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    StringReader reader = new StringReader(xml);
    return (T) unmarshaller.unmarshal(reader);
}

From source file:Main.java

public static <T> T unmarshall(String file, Class<T> desiredClass, Class context) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(context);
    Unmarshaller unMarshaller = ctx.createUnmarshaller();
    JAXBElement<T> object = (JAXBElement<T>) unMarshaller.unmarshal(new File(file));
    return object.getValue();
}

From source file:Main.java

public static Object xml2Bean(String xml, Object obj) throws Exception {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Unmarshaller um = context.createUnmarshaller();
    StringReader sr = new StringReader(xml);
    return um.unmarshal(sr);

}

From source file:Main.java

public static Object xmlStringToObject(String xmlString, Class clazz) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    StringReader reader = new StringReader(xmlString);
    return unmarshaller.unmarshal(reader);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T fromXml(String xmlStr, Class<T> klass) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(klass);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    T t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    return t;/*  w  w  w .jav a 2  s  .  com*/
}

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));
}

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

public static <T> T unMarshal(String content, Class<T> clazz) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    return unmarshaller.unmarshal(new StreamSource(new StringReader(content)), clazz).getValue();
}