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> T UnMarshall(Class<T> objectClass, String filePath) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(objectClass);
    Unmarshaller u = jc.createUnmarshaller();
    File f = new File(filePath);
    return objectClass.cast(u.unmarshal(f));
}

From source file:Main.java

public static Object parseXmlFileToBeanByJAXB(String path, Class clase) throws Exception {

    JAXBContext context = JAXBContext.newInstance(clase);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    JAXBElement elm = unmarshaller.unmarshal(new StreamSource(new File(path)), clase);

    return elm.getValue();

}

From source file:Main.java

/**
 * Create a {@link Unmarshaller} for the given context and return it.
 * /* www. j  a  va  2s.c o  m*/
 * @param jaxbContextName the context name where to look to find jaxb classes
 * @return the created {@link Unmarshaller}
 * @throws JAXBException
 */
public static Unmarshaller createUnmarshaller(final String jaxbContextName, ClassLoader cl)
        throws JAXBException {
    final JAXBContext jaxbContext = JAXBContext.newInstance(jaxbContextName, cl);
    return jaxbContext.createUnmarshaller();
}

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("rawtypes")
public static Object unmarshal(String xmlBody, Class objectClass) {
    try {/*from   w ww .  ja va 2 s  . c om*/
        JAXBContext context = JAXBContext.newInstance(objectClass);
        Unmarshaller u = context.createUnmarshaller();
        return u.unmarshal(new StreamSource(new StringReader(xmlBody)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

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

@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

@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
    T t = null;//from   ww  w.  ja 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) {
        System.out.println(e);
    }
    return t;
}