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 convertToPojoUsingString(String xml, Class... type) {
    Object result;//  w  w  w.  j a va  2  s. c  o m
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        result = unmarshaller.unmarshal(stream);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:Main.java

public static <T> T unserializer(Class<T> clazz, byte[] xml) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream is = new ByteArrayInputStream(xml);
    @SuppressWarnings("unchecked")
    T obj = (T) unmarshaller.unmarshal(is);
    return obj;/*w w w .j a  va  2s.  co  m*/
}

From source file:Main.java

/**
 * Unmarshal.//from   www .j  ava2 s . co m
 *
 * @param inputSource the input source
 * @param clazz the clazz
 * @return the object
 * @throws JAXBException the jAXB exception
 */
public static Object unmarshal(InputSource inputSource, Class<?> clazz) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller u = jc.createUnmarshaller();
    ValidationEventCollector vec = new ValidationEventCollector();
    u.setEventHandler(vec);
    Object o1 = u.unmarshal(inputSource);
    return o1;

}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshaller(String xml, Class<?> T) {
    JAXBContext jc;//from   w w  w.  j  a  va 2  s  .c  o m
    Unmarshaller unmarshaller;
    Object o = null;
    try {
        jc = JAXBContext.newInstance(T);
        unmarshaller = jc.createUnmarshaller();
        o = unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return (T) o;
}

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

public static Definitions loadTosca(String path) throws JAXBException, IOException {

    JAXBContext context = JAXBContext.newInstance(CONTEXT_TOSCA);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    return (Definitions) unmarshaller.unmarshal(Utils.loadFileFromSystem(path));
}

From source file:eu.eexcess.partnerdata.evaluation.enrichment.PartnerRecommenderEvaluationTestHelper.java

static public ResultList parseResponse(String responseString) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(ResultList.class);

    StringReader reader = new StringReader(responseString);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Object resultListObject = unmarshaller.unmarshal(reader);

    if (resultListObject instanceof ResultList) {
        return (ResultList) resultListObject;

    }/*from w w  w.j  a  v a 2  s .  c o  m*/
    return null;
}

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:Main.java

public static JAXBElement<?> 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  v  a  2 s .co m*/
    return element;
}

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:eurecom.common.util.WSUtils.java

/**
 * Read an XML file and transforms XML data into RDF data
 * @param file/*  w w  w .j  av a2 s .c o  m*/
 * @return
 * @throws JAXBException
 * @throws FileNotFoundException
 */
public static Measurements readXMLFile(String file) throws JAXBException, FileNotFoundException {
    JAXBContext context = JAXBContext.newInstance(Measurements.class);
    Unmarshaller um = context.createUnmarshaller();
    Measurements measurement = (Measurements) um.unmarshal(new FileReader(file));
    return measurement;
}