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 Object importXmlJAXB(Class<?>[] clazz, InputStream in) throws JAXBException {
    JAXBContext jc = getJAXBContext(clazz);
    Unmarshaller u = jc.createUnmarshaller();
    return u.unmarshal(in);
}

From source file:Main.java

public static Object importXmlJAXB(Class<?>[] clazz, Reader in) throws JAXBException {
    JAXBContext jc = getJAXBContext(clazz);
    Unmarshaller u = jc.createUnmarshaller();
    return u.unmarshal(in);
}

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

public static Object xmlToJaxb(Class<?> xmlClass, InputStream is) throws JAXBException, IOException {
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    JAXBElement<?> element = (JAXBElement<?>) unmarshaller.unmarshal(getReader(is));
    return element.getValue();
}

From source file:Main.java

public static Object fromXml(@SuppressWarnings("rawtypes") Class clazz, String stringXml) {
    JAXBContext context = null;
    try {/*from  www.  j av  a2 s . c o m*/
        context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return unmarshaller.unmarshal(new StreamSource(new StringReader(stringXml)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Attempts to unmarshal an XML document from an InputStream.
 *
 * @param inputStream InputStream to unmarshal XML from.
 * @param type        JAXB class of the returned object. Must be in the same package as other relevant JAXB classes.
 * @param <T>         the type of the returned object
 * @return the unmarshalled JAXB object/*from   w  w w  . j  a va 2  s.c  o m*/
 * @throws JAXBException if an unexpected error occurs while unmarshalling
 */
public static <T> T unmarshal(InputStream inputStream, Class<T> type) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(type.getPackage().getName());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    @SuppressWarnings("unchecked")
    JAXBElement<T> jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(inputStream);
    return jaxbElement.getValue();
}

From source file:Main.java

public static Object xmlToJaxb(Class<?> xmlClass, String xml) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    JAXBElement<?> element;
    try (StringReader reader = new StringReader(xml)) {
        element = (JAXBElement<?>) unmarshaller.unmarshal(reader);
    }/*from   w w w .  j  a va2 s. c o m*/
    return element.getValue();
}

From source file:Main.java

/**
 * XML to Object//from w ww. j a  v  a 2  s.  c om
 * @param <T>
 * @param clazz
 * @param reader
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T convertToObject(Class<T> clazz, Reader reader) {
    try {
        if (!uMap.containsKey(clazz)) {
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            uMap.put(clazz, unmarshaller);
        }
        return (T) uMap.get(clazz).unmarshal(reader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object importXmlJAXB(Class<?>[] clazz, Node node) throws JAXBException {
    long start = System.currentTimeMillis();
    try {// ww w  .j  av a2 s.  c  o  m
        JAXBContext jc = getJAXBContext(clazz);
        Unmarshaller u = jc.createUnmarshaller();
        return u.unmarshal(node);
    } catch (JAXBException ex) {
        throw ex;
    } finally {

    }

}

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 . j  a v  a2  s . co 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;
}