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:demo.jaxrs.util.Marshal.java

public static <T> T unmarshal(Class<T> xmlType, HttpResponse httpResponse) throws JAXBException {
    String namespace = "";
    try {/*from w  w w  .jav a2 s. c o  m*/
        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:Main.java

public static <T> T deserialize(Class<T> res, InputStream is) throws JAXBException {
    String pkg = res.getPackage().getName();
    JAXBContext jc = getCachedContext(pkg);
    Unmarshaller u = jc.createUnmarshaller();
    u.setEventHandler(new DefaultValidationEventHandler());
    return res.cast(u.unmarshal(is));
}

From source file:Main.java

public static Object unmarshall(String cntxtPkg, InputStream in)
        throws JAXBException, SAXException, IOException {
    Unmarshaller unmarshaller = createUmarshall(cntxtPkg);
    if (unmarshaller == null)
        return null;
    return unmarshaller.unmarshal(in);
    // JAXBElement<?> element = (JAXBElement<?>) unmarshaller.unmarshal(in);
    ///* w w  w  .j av  a 2s.  c  o  m*/
    // return element.getValue();
}

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 {//  w  ww.  j  ava 2  s.com
        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;
}

From source file:com.htmlhifive.tools.jslint.engine.option.xml.JaxbUtil.java

/**
 * xml?./*  www.j a v a  2s  . c om*/
 * 
 * @param is ?inputStream
 * @return ??.
 */
public static JsCheckOption readJsCheckOption(InputStream is) {
    try {
        Unmarshaller um = jc.createUnmarshaller();
        return (JsCheckOption) um.unmarshal(is);
    } catch (JAXBException e) {
        logger.put(Messages.EM0100, e);
        return null;
    }
}

From source file:de.hybris.platform.b2b.punchout.PunchOutUtils.java

public static CXML unmarshallCXMLFromFile(final String relativeFilePath) throws FileNotFoundException {
    final InputStream fileInputStream = PunchOutUtils.class.getClassLoader()
            .getResourceAsStream(relativeFilePath);

    if (fileInputStream == null) {
        throw new FileNotFoundException("Could not find file [" + relativeFilePath + "]");
    }// ww  w. j  a  v  a2 s . com

    try {
        final JAXBContext jaxbContext = JAXBContext.newInstance(CXML.class);
        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        return (CXML) unmarshaller.unmarshal(fileInputStream);
    } catch (final JAXBException e) {
        throw new PunchOutException(e.getErrorCode(), e.getMessage());
    }
}

From source file:Main.java

/**
 * This method makes the conversion XML -> JAVA with JAXB.
 * // w ww  .j  a  va 2 s. c om
 * @param xml
 *            The XML to convert.
 * @param classType
 *            The target class to convert.
 * @param <E>
 *            Class type that resolves to the given XML's NameSpace.
 * @return The java object representation of the passed XML.
 * @throws JAXBException
 *             If some problem occur while making the conversion.
 */
@SuppressWarnings("unchecked")
public static <E> E getObjectByXML(String xml, Class<E> classType) throws JAXBException {
    E retObj = null;
    if (xml != null) {
        Unmarshaller unmarshaller = getJaxbContext(classType).createUnmarshaller();
        retObj = (E) unmarshaller.unmarshal(new StringReader(stripNonValidXMLCharacters(xml)));
    }
    return retObj;
}

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

public static <T> T unmarshal(Class<T> xmlType, HttpResponse httpResponse) throws JAXBException {
    String namespace = "";
    try {//from   w ww  .  j  a va 2s.co  m
        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(Constants.DATA_SERVICE_XMLNS, "");
    System.out.println(namespace);
    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlType);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    return (T) unmarshaller.unmarshal(stream);
}

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

public static <T> T unmarshal(Class<T> xmlType, final String xmlString) {
    String namespace = xmlString;
    System.out.println(namespace);

    namespace = namespace.replaceAll(Constants.DATA_SERVICE_XMLNS, "");
    System.out.println(namespace);

    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext;//from  w  w  w .j  a v  a  2 s.  co m
    T t = null;
    try {
        jaxbContext = JAXBContext.newInstance(xmlType);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(stream);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return t;
}

From source file:Main.java

/**
 * Attempts to unmarshal an XML document from an InputStream.
 *
 * @param inputStream InputStream to unmarshal XML from.
 * @param type        JAXB class of the returned object. Must be in the same package as other relevant JAXB classes.
 * @param <T>         the type of the returned object
 * @return the unmarshalled JAXB object//  w w w . j  av  a 2 s .  c om
 * @throws JAXBException if an unexpected error occurs while unmarshalling
 */
public static <T> T unmarshal(InputStream inputStream, Class<T> type) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(type.getPackage().getName());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    @SuppressWarnings("unchecked")
    JAXBElement<T> jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(inputStream);
    return jaxbElement.getValue();
}