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:at.ac.tuwien.dsg.comot.m.cs.UtilsCs.java

public static Requirements loadRequirements(String path) throws JAXBException, IOException {

    JAXBContext context = JAXBContext.newInstance(Requirements.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    return (Requirements) unmarshaller.unmarshal(Utils.loadFileFromSystem(path));

}

From source file:com.topsem.common.mapper.JaxbMapper.java

/**
 * UnMarshaller./*from   www.  j  a va  2  s . c  om*/
 * ???pooling
 */
public static Unmarshaller createUnmarshaller(Class clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        return jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.wsun.seap.common.mapper.JaxbMapper.java

/**
 * UnMarshaller.//from   w ww  .j a  v  a 2s  .c  o m
 * ???pooling
 */
public static Unmarshaller createUnmarshaller(Class clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        return jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        throw ExceptionUtil.unchecked(e);
    }
}

From source file:com.androidwhy.modules.mapper.JaxbMapper.java

/**
 * UnMarshaller.//w w w.  j  a  va2s. com
 * ???pooling
 */
public static Unmarshaller createUnmarshaller(Class clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        return jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:Main.java

public static <T> T fromXml(String xml, Class<T> type) {
    if (xml == null || xml.trim().equals("")) {
        return null;
    }//from  w ww  . j  a  v  a  2s  . c o  m
    JAXBContext jc = null;
    Unmarshaller u = null;
    T object = null;
    try {
        jc = JAXBContext.newInstance(type);
        u = jc.createUnmarshaller();
        object = (T) u.unmarshal(new ByteArrayInputStream(xml.getBytes(ENCODING)));
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    return object;
}

From source file:Main.java

/**
 * Unmarshal XML data from XML file path using XSD from file path and return the resulting JAXB content tree
 *
 * @param dummyCtxObject//  w w w  .  ja v a2  s  .c  o  m
 *            Dummy contect object for creating related JAXB context
 * @param strXMLFilePath
 *            XML file path
 * @param strXSDFilePath
 *            XSD file path
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshallingFromFiles(Object dummyCtxObject, String strXMLFilePath,
        String strXSDFilePath) throws Exception {
    if (dummyCtxObject == null) {
        throw new RuntimeException("No dummy context object (null)!");
    }
    if (strXMLFilePath == null) {
        throw new RuntimeException("No XML file path (null)!");
    }
    if (strXSDFilePath == null) {
        throw new RuntimeException("No XSD file path (null)!");
    }

    Object unmarshalledObject = null;

    FileInputStream fis = null;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        // unmarshaller.setValidating(true);
        javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new java.io.File(strXSDFilePath));
        unmarshaller.setSchema(schema); // register schema for validation

        fis = new FileInputStream(strXMLFilePath);
        unmarshalledObject = unmarshaller.unmarshal(fis);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (fis != null) {
            fis.close();
            fis = null;
        }
    }

    return unmarshalledObject;
}

From source file:at.ac.tuwien.dsg.comot.m.cs.UtilsCs.java

public static MonitoredElement loadMonitoredElement(String path) throws JAXBException, IOException {

    JAXBContext context = JAXBContext.newInstance(MonitoredElement.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    return (MonitoredElement) unmarshaller.unmarshal(Utils.loadFileFromSystem(path));

}

From source file:com.plateform.common.util.JaxbMapper.java

/**
 * UnMarshaller./*from   www.  j  a v  a 2 s .c o  m*/
 * ???pooling
 */
public static Unmarshaller createUnmarshaller(Class clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        return jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:Main.java

/**
 * Unmarshal XML data from XML DOM document using XSD string and return the resulting JAXB content tree
 *
 * @param dummyJAXBObject//from   w  w w.j  av  a 2  s. co m
 *            Dummy contect object for creating related JAXB context
 * @param doc
 *            XML DOM document
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshallingFromDOMDocument(Object dummyJAXBObject, Document doc, String strXSD)
        throws Exception {
    if (dummyJAXBObject == null) {
        throw new RuntimeException("No dummy context objekt (null)!");
    }
    if (doc == null) {
        throw new RuntimeException("No XML DOM document (null)!");
    }
    if (strXSD == null) {
        throw new RuntimeException("No XSD document (null)!");
    }

    Object unmarshalledObject = null;
    StringReader reader = null;

    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyJAXBObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        reader = new StringReader(strXSD);

        javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(reader));
        unmarshaller.setSchema(schema);

        unmarshalledObject = unmarshaller.unmarshal(doc);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }

    return unmarshalledObject;
}

From source file:eu.planets_project.tb.impl.serialization.JaxbUtil.java

public static <T> T unmarshallObjectViaJaxb(Class<T> objectClass, String serializedObject) throws Exception {
    JAXBContext context;
    try {//from w w  w.ja v a 2  s .c  om
        context = JAXBContext.newInstance(objectClass);
        Unmarshaller um = context.createUnmarshaller();
        return (T) um.unmarshal(new StringReader(serializedObject));
    } catch (JAXBException e) {
        log.error("unmarshalWorkflowResult failed for objectClass" + objectClass + " with " + e);
        throw e;
    }
}