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 unmarshall(Class<T> clazz, Node node, Unmarshaller.Listener listener) throws JAXBException {
    Unmarshaller unmarshaller = getUnmarshaller(clazz, listener);
    return (T) unmarshaller.unmarshal(node);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshall(Class<T> clazz, Reader reader, Unmarshaller.Listener listener)
        throws JAXBException {
    Unmarshaller unmarshaller = getUnmarshaller(clazz, listener);
    return (T) unmarshaller.unmarshal(reader);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshall(Class<T> clazz, InputStream inputStream, Unmarshaller.Listener listener)
        throws JAXBException {
    Unmarshaller unmarshaller = getUnmarshaller(clazz, listener);
    return (T) unmarshaller.unmarshal(inputStream);
}

From source file:Main.java

public static <T> T xml2Obj(String xmlString, Class<T> clazz) throws RuntimeException {
    T c = null;/*from  w  ww  . j  av  a2s .c  o m*/
    Reader reader = null;
    try {
        reader = new StringReader(xmlString);
        Unmarshaller unMarshaller = JAXBContext.newInstance(clazz).createUnmarshaller();
        c = (T) unMarshaller.unmarshal(reader);
    } catch (JAXBException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return c;
}

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 <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 parse(final Class<T> clazz, final InputStream inputStream) {
    try {/*  www. j ava  2 s.  c o m*/
        final JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        final Object deserialized = jaxbUnmarshaller.unmarshal(inputStream);
        if (clazz.isAssignableFrom(deserialized.getClass())) {
            return clazz.cast(deserialized);
        } else {
            final JAXBElement<T> jaxbElement = (JAXBElement<T>) deserialized;
            return jaxbElement.getValue();
        }
    } catch (final JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Product.java

public static Product fromXML(InputStream in) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Product.class);
    Unmarshaller um = context.createUnmarshaller();
    return (Product) um.unmarshal(in);
}

From source file:Main.java

/**
 * Converts an input XML reader into the given type.
 * //  w  w  w .  ja va  2 s . com
 * @param <T>
 *            the type to parse the XML into
 * @param xml
 *            the XML to convert
 * @param clazz
 *            the type to parse the XML into
 * @return the xml data converted into the specified type
 * @throws JAXBException
 *             XML Exception thrown if the conversion failed
 */
@SuppressWarnings("unchecked")
public static synchronized <T> T fromXml(Reader xml, Class<T> clazz) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    return (T) unmarshaller.unmarshal(xml);
}

From source file:com.radialpoint.uima.typemapper.RulesFileLoader.java

public static Rules loadRulesFromStream(InputStream stream) throws JAXBException, FileNotFoundException {

    Rules rules = new Rules();

    // Unmarshal/*from w  ww  .jav  a2  s.  c  o  m*/
    JAXBContext jc = JAXBContext.newInstance(Rules.class, Rule.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    rules = (Rules) unmarshaller.unmarshal(stream);

    if (rules == null || CollectionUtils.isEmpty(rules.getRuleList())) {
        throw new JAXBException("Rules list is empty. Binding error?");
    }

    return rules;
}