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 parse(final Class<T> clazz, final InputStream inputStream) {
    try {//from   ww w  . j ava  2s  .  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:Main.java

public static <T> T loadObject(Class<T> typeClass, URL path) {
    T object = null;//ww w  .j  ava2  s.  c om

    try {
        File file = new File(path.toURI());
        JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        object = (T) jaxbUnmarshaller.unmarshal(file);
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    return object;
}

From source file:Main.java

private static <T> Unmarshaller getUnmarshaller(Class<T> clazz, Unmarshaller.Listener listener)
        throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    if (listener != null) {
        unmarshaller.setListener(listener);
    }//w  w  w  .  j a v  a 2s  . co  m
    return unmarshaller;
}

From source file:Main.java

/**
 * Converts the XML file specified into the specified POJO type
 * @param <T> the object type of the POJO
 * @param xmlfile the XML file to convert
 * @param classOfT the class of the POJO
 * @return the POJO object if conversion was successful
 * @throws JAXBException/*from  w w  w  .  ja va  2  s. c  om*/
 * @throws XMLStreamException
 * @throws FileNotFoundException 
 */
public static <T> T convertToPojo(File xmlfile, Class<T> classOfT)
        throws JAXBException, XMLStreamException, FileNotFoundException {
    JAXBContext jaxbContext = JAXBContext.newInstance(classOfT);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    XMLInputFactory xif = XMLInputFactory.newFactory();
    // settings to prevent xxe // would be funny if this tool is itsef is vulnerable to xxe :D
    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);

    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader(xmlfile));
    T t = (T) jaxbUnmarshaller.unmarshal(xsr);//(xmlfile);

    return t;
}

From source file:Main.java

/**
 * Convert XML data to a bean using JAXB.
 * //  w  w w. j  a  va2  s  .c  o m
 * @param b
 *            The bean, represented as XML.
 * @param implClass
 *            The implementation class of the bean.
 * @param bc
 *            Additional classes to add to the JAXB context.
 * @return The bean, unmarshalled from the XML data using JAXB.
 */
public static <T> T unmarshal(String b, Class<T> implClass, Class<?>... bc) {
    Class<?>[] bind;
    if (bc.length > 1) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = implClass;
        for (int i = 0; i < bc.length; i++) {
            bind[i + 1] = bc[i];
        }
    } else {
        bind = new Class<?>[] { implClass, };
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(b.getBytes());
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        return implClass.cast(unmarshaller.unmarshal(bais));
    } catch (JAXBException e) {
        throw new IllegalStateException("Error unmarshalling", e);
    }
}

From source file:se.inera.intyg.intygstjanst.web.integration.util.SendMessageToCareUtil.java

public static SendMessageToCareType getSendMessageToCareTypeFromFile(String fileName) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(SendMessageToCareType.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return unmarshaller.unmarshal(new StreamSource(new ClassPathResource(fileName).getInputStream()),
            SendMessageToCareType.class).getValue();
}

From source file:Main.java

/**
 * Converts an input XML reader into the given type.
 * /*ww w.  j ava  2  s  .  co m*/
 * @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:Main.java

/**
 * Loads the object from the file./*  w  w w  .j av a  2  s.  c o  m*/
 *
 * @param <T> the object type.
 * @param path the XML file.
 * @param clazz the class of the object.
 * @return the corresponding object.
 */
public static <T> T loadObject(Path path, Class<T> clazz) {
    T result;
    if (path == null || clazz == null) {
        throw new RuntimeException("The path to file or class is null!");
    }

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        result = (T) jaxbUnmarshaller.unmarshal(path.toFile());
    } catch (Exception ex) {
        throw new RuntimeException("Error loading the xml from path " + path.toString(), ex);
    }
    return result;
}

From source file:Main.java

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

From source file:Main.java

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