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:eu.hydrologis.jgrass.featureeditor.utils.Utilities.java

/**
 * Parse a form xml.// w  w  w. j a v a 2 s  . co m
 * 
 * @param xml the xml containing the form definition. 
 * @return the {@link AForm}.
 * @throws Exception
 */
public static AForm parseXML(String xml) throws Exception {
    JAXBContext jc = JAXBContext.newInstance("eu.hydrologis.jgrass.featureeditor.xml.annotated"); //$NON-NLS-1$
    Unmarshaller um = jc.createUnmarshaller();
    StringReader sr = new StringReader(xml);
    return (AForm) um.unmarshal(sr);
}

From source file:com.labs64.utils.swid.support.JAXBUtils.java

public static <T> T readObjectFromInputStream(final InputStream inputStream, final Class<T> expectedType) {
    try {//from w  w  w . j  a v  a  2s  .  c o  m
        JAXBContext jaxbContext = JAXBContext.newInstance(expectedType);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        JAXBElement<T> element = unmarshaller.unmarshal(new StreamSource(inputStream), expectedType);
        return element.getValue();
    } catch (final JAXBException e) {
        throw new SwidException("Cannot process resource.", e);
    }
}

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

public static CompositionRulesConfiguration loadMetricCompositionRules(String serviceId, String path)
        throws JAXBException, IOException {

    CompositionRulesConfiguration xmlContent = null;

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

    xmlContent = (CompositionRulesConfiguration) unmarshaller.unmarshal(Utils.loadFileFromSystem(path));
    xmlContent.setTargetServiceID(serviceId);

    return xmlContent;
}

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

/**
 * @param in//from   ww  w. j av  a2 s .c  om
 * @return
 */
private static ExperimentImpl readFromInputStream(InputStream in) {
    try {
        JAXBContext jc = JAXBContext.newInstance(PACKAGE_CONTEXT);
        Unmarshaller u = jc.createUnmarshaller();
        ExperimentImpl exp = (ExperimentImpl) u.unmarshal(in);
        return exp;
    } catch (JAXBException e) {
        log.fatal("Reading Experiment from XML failed: " + e);
        return null;
    }
}

From source file:be.fedict.eid.tsl.TrustServiceListFactory.java

private static Unmarshaller getUnmarshaller() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return unmarshaller;
}

From source file:eu.seaclouds.platform.dashboard.util.ObjectMapperHelpers.java

/**
 * Transforms a XML string to an Object using javax.xml.bind.Unmarshaller.
 * If you want to parse a collection please @see {#link XmlToObjectCollection}
 *
 * @param xml//from w  w  w  .ja v a2  s. c om
 * @param type Class object representing the target class T
 * @param <T>  target class
 * @return an instance of T
 * @throws IOException if is not possible to parse the object
 */
public static <T> T XmlToObject(String xml, Class<T> type) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(type);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    T obj = (T) jaxbUnmarshaller.unmarshal(new StringReader(xml));
    return obj;
}

From source file:com.faceye.feature.util.JaxbMapper.java

/**
 * UnMarshaller.//from  w w  w  .j a va  2s .  c o  m
 * ???pooling
 */
public static Unmarshaller createUnmarshaller(Class clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        return jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        logger.error(">>FaceYe form xml 2 class exception:", e);
        throw Exceptions.unchecked(e);
    }
}

From source file:Main.java

/**
 * Unmarshal XML data from XML file path using XSD string and return the resulting JAXB content tree
 * /*  w  ww.  j a  va  2 s .  c  o m*/
 * @param dummyCtxObject
 *            Dummy contect object for creating related JAXB context
 * @param strXMLFilePath
 *            XML file path
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshallingFromXMLFile(Object dummyCtxObject, String strXMLFilePath, String strXSD)
        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 (strXSD == null) {
        throw new RuntimeException("No XSD (null)!");
    }

    Object unmarshalledObject = 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(m_strXSDFilePath));
         */
        StringReader reader = null;
        FileInputStream fis = null;
        try {
            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);

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

    return unmarshalledObject;
}

From source file:Main.java

private static Unmarshaller createUmarshall(String pkgName) throws JAXBException, SAXException {
    JAXBContext jaxbCtx = null;
    if ((jaxbCtx = marshallContexts.get(pkgName)) == null) {
        jaxbCtx = JAXBContext.newInstance(pkgName);
        marshallContexts.put(pkgName, jaxbCtx);
    }/*  w  ww .ja  v  a2s  .c o  m*/
    Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();

    return unmarshaller;
}

From source file:att.jaxrs.util.Marshal.java

public static <T> T unmarshal(Class<T> xmlType, final String xmlString) {
    String namespace = xmlString;
    System.out.println(namespace);

    namespace = namespace.replaceAll(Constants.DATA_SERVICE_XMLNS, "");
    System.out.println(namespace);

    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext;
    T t = null;/*from w  ww. ja v a2  s  .c  o m*/
    try {
        jaxbContext = JAXBContext.newInstance(xmlType);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(stream);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return t;
}