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

@SuppressWarnings("unchecked")
public static <T> T asObjectFromJson(String str, Class<T> clazz, Class<?>... otherClazz) throws JAXBException {

    List<Object> list = new ArrayList<Object>(Arrays.asList(otherClazz));
    list.add(clazz);/*  w ww.  j av  a2 s. co m*/

    Map<String, Object> props = new HashMap<String, Object>();
    props.put(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);

    JAXBContext context = JAXBContextFactory.createContext(list.toArray(new Class[list.size()]), props);
    Unmarshaller unm = context.createUnmarshaller();

    return (T) unm.unmarshal(new StringReader(str));
}

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  . j  a  v a 2s .  c  o m
 * @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:Main.java

public static <T> T fromXml(String xml, Class<T> type) {
    if (xml == null || xml.trim().equals("")) {
        return null;
    }//from ww  w . j  a v a 2 s .  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

/**
 * Marshal a object of//w  w w  . j  a  va 2 s  .c  o m
 * <code>classItem</code> from the xmlResponse
 * <code>String</code>.
 *
 * @param xmlResponse <code>String</code> that represents the object to be
 *                    marshal.
 * @param classItem   <code>class</code> of the returns object.
 * @return a object of <code>classItem</code>.
 * @throws JAXBException throw trying to marshal.
 */
public static Object unmarshalFromString(String xmlResponse, Class classItem) throws JAXBException {
    final JAXBContext jaxbContext = JAXBContext.newInstance(classItem);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    StringReader reader = null;
    try {
        reader = new StringReader(xmlResponse);
        return jaxbUnmarshaller.unmarshal(reader);
    } finally {
        if (reader != null)
            reader.close();
    }
}

From source file:Main.java

/**
 * Marshal a object of/*from www .ja v a  2 s .c  o m*/
 * <code>classItem</code> from the xmlResponse
 * <code>String</code>.
 *
 * @param xmlResponse <code>String</code> that represents the object to be
 *                    marshal.
 * @param classItem   <code>String</code> of the returns object.
 * @return a object of <code>classItem</code>.
 * @throws JAXBException throw trying to marshal.
 */
public static Object unmarshalFromString(String xmlResponse, String classItem) throws JAXBException {
    final JAXBContext jaxbContext = JAXBContext.newInstance(classItem);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    StringReader reader = null;
    try {
        reader = new StringReader(xmlResponse);
        return jaxbUnmarshaller.unmarshal(reader);
    } finally {
        if (reader != null)
            reader.close();
    }
}

From source file:Main.java

public static <T> T xmlToObj(Class<T> t, String xml, String encoding) {
    try {//from w w w  . ja  v a  2s  .co  m
        JAXBContext jaxbContext = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(encoding));
        @SuppressWarnings("unchecked")
        T obj = (T) unmarshaller.unmarshal(bais);
        bais.close();
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:at.ac.tuwien.dsg.comot.m.common.Utils.java

@SuppressWarnings("unchecked")
public static <T> T asObjectFromXml(String str, Class<T> clazz, Class<?>... otherClazz)
        throws JAXBException, IOException {

    List<Object> list = new ArrayList<Object>(Arrays.asList(otherClazz));
    list.add(clazz);/*from   w  w w.j a v a  2  s.co  m*/

    JAXBContext context = JAXBContext.newInstance(list.toArray(new Class[list.size()]));
    Unmarshaller unmarshaller = context.createUnmarshaller();

    return (T) unmarshaller.unmarshal(new StringReader(str));
}

From source file:com.esri.geoevent.test.performance.OrchestratorMain.java

private static Fixtures fromXML(String xmlLocation) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Fixtures.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    StreamSource xml = new StreamSource(xmlLocation);
    return (Fixtures) unmarshaller.unmarshal(xml);
}

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

/**
 * @param in/*from  w  w w .  j a va 2  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;
    }
}