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

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);
    }// w ww.ja v  a  2 s. c  om
    return element.getValue();
}

From source file:Main.java

/**
 *
 * @param fXMLFilePath/*from   ww w. j ava2s  .c om*/
 * @param cls
 * @return
 * @throws JAXBException
 */
public static Object deserialize(File fXMLFilePath, Class cls) throws JAXBException {
    final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
    return um.unmarshal(fXMLFilePath);
}

From source file:Main.java

/**
 *
 * @param xml//from w w w  .  j  av a 2s. c  o m
 * @param cls
 * @return
 * @throws JAXBException
 */
public static Object deserialize(String xml, Class cls) throws JAXBException {
    final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
    return um.unmarshal(new ByteArrayInputStream(xml.getBytes()));
}

From source file:Main.java

/**
 *
 * @param io//from www. jav a2 s  .c om
 * @param cls
 * @return
 * @throws JAXBException
 */
public static Object deserialize(InputStream io, Class cls) throws JAXBException {
    final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
    return um.unmarshal(io);
}

From source file:Main.java

/**
 *
 * @param elmnt/*from  w w  w.j  a v a2s  . c  o m*/
 * @param cls
 * @return
 * @throws JAXBException
 */
public static Object deserialize(Element elmnt, Class cls) throws JAXBException {
    final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
    return um.unmarshal(elmnt);
}

From source file:org.pmedv.jake.JakeUtil.java

/**
 * Updates the {@link RecentFileList} with a new file
 * /*from   w  w  w. j  av a2 s. c o m*/
 * @param filename the name to append to the list
 */
public static void updateRecentFiles(String filename) {

    RecentFileList fileList = null;

    try {

        String inputDir = System.getProperty("user.home") + "/." + AppContext.getName() + "/";
        String inputFileName = "recentFiles.xml";
        File inputFile = new File(inputDir + inputFileName);

        if (inputFile.exists()) {
            Unmarshaller u = JAXBContext.newInstance(RecentFileList.class).createUnmarshaller();
            fileList = (RecentFileList) u.unmarshal(inputFile);
        }

        if (fileList == null)
            fileList = new RecentFileList();

    } catch (JAXBException e) {
        e.printStackTrace();
    }

    if (fileList.getRecentFiles().size() >= 5) {
        fileList.getRecentFiles().remove(0);
    }

    if (!fileList.getRecentFiles().contains(filename))
        fileList.getRecentFiles().add(filename);

    Marshaller m;

    try {
        String outputDir = System.getProperty("user.home") + "/." + AppContext.getName() + "/";
        String outputFileName = "recentFiles.xml";
        m = JAXBContext.newInstance(RecentFileList.class).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        File output = new File(outputDir + outputFileName);
        m.marshal(fileList, output);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:cz.lbenda.dataman.rc.DbConfigFactory.java

public static void load(final String document) {
    if (StringUtils.isBlank(document)) {
        return;//from  ww w  .  j a  v a  2s .  co  m
    }
    try {
        JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.dataman.ObjectFactory.class);
        Unmarshaller u = jc.createUnmarshaller();
        JAXBElement o = (JAXBElement) u.unmarshal(new StringReader(document));
        if (o.getValue() instanceof DatamanType) {
            DatamanType dc = (DatamanType) o.getValue();
            if (dc.getSessions() == null || dc.getSessions().getSession().isEmpty()) {
                LOG.info("No configuration for loading");
            } else {
                configurations.clear();
                for (SessionType session : dc.getSessions().getSession()) {
                    DbConfig sc = new DbConfig();
                    configurations.add(sc);
                    sc.fromSessionType(session, true);
                }
            }
        } else {
            LOG.error("The string didn't contains dataman configuration: " + o.getClass().getName());
        }
    } catch (JAXBException e) {
        LOG.error("Problem with reading dataman configuration\nXML:" + document + "\n" + e.toString(), e);
    }
}

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

private static TrustStatusListType parseTslDocument(Document tslDocument) throws JAXBException {
    Unmarshaller unmarshaller = getUnmarshaller();
    JAXBElement<TrustStatusListType> jaxbElement = (JAXBElement<TrustStatusListType>) unmarshaller
            .unmarshal(tslDocument);/*from  w w w  .j  a v a 2s .  c om*/
    TrustStatusListType trustServiceStatusList = jaxbElement.getValue();
    return trustServiceStatusList;
}

From source file:eu.europa.esig.dss.validation.ValidationResourceManager.java

/**
 * This is the utility method that loads the data from the inputstream determined by the inputstream parameter into
 * a//  w  w w. j  av a2s  .c  o  m
 * {@link ConstraintsParameters}.
 *
 * @param inputStream
 * @return
 */
public static ConstraintsParameters load(final InputStream inputStream) throws DSSException {
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new StreamSource(
                ValidationResourceManager.class.getResourceAsStream(defaultPolicyXsdLocation)));

        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setSchema(schema);

        return (ConstraintsParameters) unmarshaller.unmarshal(inputStream);
    } catch (Exception e) {
        throw new DSSException("Unable to load policy : " + e.getMessage(), e);
    }
}

From source file:net.firejack.platform.core.utils.FileUtils.java

public static <T> T readJAXB(InputStream is, Class... clazz) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller u = jc.createUnmarshaller();

    return (T) u.unmarshal(is);
}