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

/**
 * @param file//from   ww  w. j  a  v  a2  s . c  om
 * @return
 * @throws JAXBException
 */
public static Object unmarshalXML(Class clasz, InputStream file) throws JAXBException {
    //1. We need to create JAXContext instance
    JAXBContext jaxbContext = JAXBContext.newInstance(clasz);

    //2. Use JAXBContext instance to create the Unmarshaller.
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    //3. Use the Unmarshaller to unmarshal the XML document to get an instance of JAXBElement.
    return (file != null) ? unmarshaller.unmarshal(file) : null;
}

From source file:Main.java

public static <T> T asObject(String xml, Class<T> clazz) {
    try {// w ww.  ja v a 2s. co  m
        JAXBContext jaxbContext = null;
        jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = null;
        unmarshaller = jaxbContext.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:gov.hhs.fha.nhinc.lift.proxy.util.ProxyUtil.java

public static Object unmarshal(org.w3c.dom.Element elem, Class<?> c) throws JAXBException {
    Object result = null;//w w  w  .ja v  a2  s  .  c om

    JAXBContext jc = JAXBContext.newInstance(c);
    Unmarshaller um = jc.createUnmarshaller();
    result = um.unmarshal(elem);

    return result;
}

From source file:gov.hhs.fha.nhinc.lift.proxy.util.ProxyUtil.java

public static Object unmarshalFromReader(Reader reader, Class<?> c) throws JAXBException {
    Object result = null;/*www .j  ava2  s.  c  om*/

    JAXBContext jc = JAXBContext.newInstance(c);
    Unmarshaller um = jc.createUnmarshaller();
    result = um.unmarshal(reader);

    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshaller(String xml, Class<?> T) {
    JAXBContext jc;
    Unmarshaller unmarshaller;//from  w  ww  .  jav a2  s . c o m
    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:demo.jaxrs.util.Marshal.java

public static <T> T unmarshal(Class<T> xmlType, String string) throws JAXBException {
    String namespace = "";

    namespace = new String(string);
    System.out.println(namespace);

    namespace = namespace.replaceAll(" xmlns=\"http://ws.wso2.org/dataservice\"", "");
    System.out.println(namespace);

    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlType);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    T doc = (T) unmarshaller.unmarshal(stream);
    return doc;/*from  www.  ja  v a  2  s.c  o m*/
}

From source file:Main.java

public static <T> T fromXml(String responseBody, Class<T> c) throws JAXBException, XMLStreamException {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(responseBody.getBytes());
    JAXBContext jaxbContext = JAXBContext.newInstance(c);
    XMLStreamReader xsr = xmlInputFactory.createXMLStreamReader(inputStream);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (T) jaxbUnmarshaller.unmarshal(xsr);
}

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

public static <T> T unmarshal(Class<T> xmlType, HttpResponse httpResponse) throws JAXBException {
    String namespace = "";
    try {//from www .j  a  va2s .com
        namespace = Util.getStringFromInputStream(httpResponse.getEntity().getContent());
        System.out.println(namespace);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

    System.out.println(namespace);
    namespace = namespace.replaceAll("xmlns=\"http://ws.wso2.org/dataservice\"", "");
    System.out.println(namespace);
    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlType);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    T doc = (T) unmarshaller.unmarshal(stream);
    return doc;
}

From source file:com.lolay.citygrid.BaseInvoker.java

protected static <T> T parseResults(Class<T> type, Response response) throws InvokerException {
    if (Response.Status.OK.getStatusCode() != response.getStatus()
            && Response.Status.BAD_REQUEST.getStatusCode() != response.getStatus()) {
        throw new RuntimeException(String.format("Can only parse 200 and 400 responses, the reponse was %s",
                response.getStatus()));//from  w ww . j  ava 2 s  .c  om
    }

    if (response.getEntity() != null && response.getEntity().getClass().equals(type)) {
        return type.cast(response.getEntity());
    } else if (response.getEntity() instanceof ErrorResults) {
        throw new InvokerException(ErrorResults.class.cast(response.getEntity()));
    } else if (response.getEntity() instanceof InputStream) {
        InputStream stream = InputStream.class.cast(response.getEntity());
        Object results = null;
        try {
            JAXBContext context = JAXBContext.newInstance(type, ErrorResults.class);
            results = context.createUnmarshaller().unmarshal(stream);
        } catch (JAXBException e) {
            throw new RuntimeException("Problem unmarshalling the entity", e);
        }
        if (results != null && results.getClass().equals(type)) {
            return type.cast(results);
        } else {
            throw new InvokerException(ErrorResults.class.cast(results));
        }
    } else {
        throw new RuntimeException(
                String.format("Do not know how to parse %s", response.getEntity().getClass()));
    }
}

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

public static <T> T unmarshal(Class<T> xmlType, InputStream inputStream) throws JAXBException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder out = new StringBuilder();
    try {//from ww w  .j a  v  a2s . c om
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    String namespace = out.toString();

    System.out.println(namespace);
    namespace = namespace.replaceAll("xmlns=\"http://ws.wso2.org/dataservice\"", "");
    System.out.println(namespace);
    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlType);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    T doc = (T) unmarshaller.unmarshal(stream);
    return doc;
}