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

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlStr, Class<T> t) {
    try {/*from   w w  w  .j  ava 2 s  .  com*/
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(InputStream input, Class<T> t) {
    try {/*w  ww .  j  a va2s  .c om*/
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new InputStreamReader(input, "UTF-8"));
    } catch (JAXBException | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object loadInstance(InputStream inputStream, Class instanceClass) throws JAXBException {
    Unmarshaller unmarshaller = JAXBContext.newInstance(instanceClass).createUnmarshaller();
    Object object = unmarshaller.unmarshal(inputStream);
    return object;
}

From source file:Main.java

public static <T> T unmarshalObject(final InputStream input, final Class<T> clazz) throws JAXBException {
    Unmarshaller unmarshaller = JAXBContext.newInstance(clazz).createUnmarshaller();
    return (T) unmarshaller.unmarshal(input);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(String xmlStr, Class<T> t) {
    try {//  w w  w  . ja va  2 s.  co  m
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        T ts = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
        return ts;
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Object xml2Bean(String xml, Object obj) throws Exception {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Unmarshaller um = context.createUnmarshaller();
    StringReader sr = new StringReader(xml);
    return um.unmarshal(sr);

}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(InputStream input, Class<T> t) {
    try {/*  www . j  a  va  2 s.c o m*/
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        T ts = (T) unmarshaller.unmarshal(new InputStreamReader(input, "UTF-8"));
        return ts;
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static <T> T loadObject(Class<T> typeClass, URL path) {
    T object = null;/*from   w ww  .  jav  a2 s .c om*/

    try {
        File file = new File(path.toURI());
        JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        object = (T) jaxbUnmarshaller.unmarshal(file);
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    return object;
}

From source file:Main.java

public static <T> T unmarshal(InputStream in, Class... boundClasses) {
    try {/*from w  w  w  . j  a  va  2s.c  o  m*/
        Unmarshaller u = JAXBContext.newInstance(boundClasses).createUnmarshaller();
        return (T) u.unmarshal(in);
    } catch (Exception ex) {
    }
    return null;
}

From source file:Main.java

public static Object parseRequestObjectFromSoap(String soapXml) throws Exception {
    Object result = null;//from   www.  jav a  2  s . co m

    if (soapXml != null && soapXml.trim().length() > 0) {
        DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
        xmlFact.setNamespaceAware(true);

        DocumentBuilder builder = xmlFact.newDocumentBuilder();
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        StringReader xsr = new StringReader(soapXml);
        InputSource is = new InputSource(xsr);
        Document doc = builder.parse(is);

        //Node requestNode = (Node) xpath.evaluate("/soap:Envelope/soap:Body/*", doc, XPathConstants.NODE);
        Node requestNode = (Node) xpath.evaluate("/*[local-name()='Envelope']/*[local-name()='Body']/*", doc,
                XPathConstants.NODE);

        JAXBContext ctx = JAXBContext.newInstance("xo.xoi.orderentry.ebonding");
        Unmarshaller unmarshaller = ctx.createUnmarshaller();
        result = unmarshaller.unmarshal(requestNode);
    }

    return result;
}