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 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;/* w  ww.j av  a 2  s  .c  om*/
}

From source file:Main.java

public static <T> T unserializer(Class<T> clazz, InputStream xmlInput) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    @SuppressWarnings("unchecked")
    T obj = (T) unmarshaller.unmarshal(xmlInput);
    return obj;//  w w  w .ja  v a2 s  .co m
}

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 Object convertToPojoUsingString(String xml, Class... type) {
    Object result;//w  w  w .  ja  va 2 s.c o m
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        result = unmarshaller.unmarshal(stream);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T convertToObject(String xml, Class<T> type) {
    StringReader sr = new StringReader(xml);
    try {//from  w ww . ja va  2 s .  c o m
        JAXBContext jAXBContext = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
        return (T) unmarshaller.unmarshal(sr);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

}

From source file:Main.java

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

From source file:Main.java

public final static Object load(Class clazz, String name) {
    try {//w w  w .ja  v  a  2  s. 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

@SuppressWarnings("unchecked")
public static <T> T readXMLFromString(Class<?> class1, String content)
        throws JAXBException, FileNotFoundException, SAXException, ParserConfigurationException {
    JAXBContext context = JAXBContext.newInstance(class1);
    Unmarshaller um = context.createUnmarshaller();

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    spf.setFeature("http://xml.org/sax/features/validation", false);

    XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader();
    try (StringReader reader = new StringReader(content)) {
        SAXSource source = new SAXSource(xr, new InputSource(reader));

        T obj = (T) um.unmarshal(source);
        return obj;
    }//www  . jav a 2 s  .  c o  m
}

From source file:Main.java

/**
 * @since 2.0/*from  www .  j  a v a2 s .  c o  m*/
 */
public static Unmarshaller createUnmarshaller(final Class<?> bind) throws JAXBException {
    // HACK: random ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl (WTF?)
    synchronized (bind) {
        synchronized (JAXBContext.class) {
            JAXBContext context = createContext(bind);

            return context.createUnmarshaller();
        }
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> Object xmlToObject(InputStream xmlContent, Class<T> clazz) throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setSchema(null); //note: setting schema to null will turn validator off
    Object unMarshalled = unmarshaller.unmarshal(xmlContent);
    Object xmlObject = clazz.cast(unMarshalled);

    return (T) xmlObject;
}