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

/**
 * Marshal a object of//  w ww.  ja  v a 2 s. co 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/*w  ww.  java 2  s  .  c om*/
 * <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

/**
 * Converting DOM Element object into JAXB OASIS XML Object
 * @param <T>// w  w  w  .  j a  va 2s  .c  o m
 * @param cls
 * @param domElement
 * @return
 */
public static <T> T marshal(Class<T> cls, Element domElement) {
    try {
        JAXBContext jc = JAXBContext.newInstance(cls);
        javax.xml.bind.Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<T> jaxbObject = unmarshaller.unmarshal(domElement, cls);

        T object = jaxbObject.getValue();

        return object;
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object parseRequestObjectFromSoap(String soapXml) throws Exception {
    Object result = null;/* w ww . j  a  va  2s  .c o  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;
}

From source file:Main.java

public static <T> T converyToJavaBean(JAXBContext context, String xmlStr, Class<T> c) {
    JAXBElement<T> t = null;
    try {//from  w w  w . j a va  2  s  .  co m
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (JAXBElement<T>) unmarshaller.unmarshal((new StreamSource(new StringReader(xmlStr))), c);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return t.getValue();
}

From source file:Main.java

/**
 * XML to Object//from w  ww .j a  v  a  2s. co  m
 * @param <T> T
 * @param clazz clazz
 * @param reader reader
 * @return T
 */
@SuppressWarnings("unchecked")
public static <T> T convertToObject(Class<T> clazz, Reader reader) {
    try {
        Map<Class<?>, Unmarshaller> uMap = uMapLocal.get();
        if (!uMap.containsKey(clazz)) {
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            uMap.put(clazz, unmarshaller);
        }
        return (T) uMap.get(clazz).unmarshal(reader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T read(String input, Class<T> typeParameterClass) {
    T content = null;/*from   w  w  w .ja v  a  2  s .com*/
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        //jaxbUnmarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        InputStream is = new ByteArrayInputStream(input.getBytes());
        content = (T) jaxbUnmarshaller.unmarshal(is);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return content;
}

From source file:Main.java

/**
 * Reads an XML file and returns the content as an object of the specified class. 
 * @param file               XML file.//from ww w. j  a  v  a  2s.  c  om
 * @param typeParameterClass   Class of the main object in the XML file.
 * @return                  Content as an object of the specified class.
 */
@SuppressWarnings("unchecked")
public static <T> T read(File file, Class<T> typeParameterClass) {
    T content = null;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        //jaxbUnmarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        content = (T) jaxbUnmarshaller.unmarshal(file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return content;
}

From source file:Main.java

/**
 * /* w ww  .  ja va 2 s.  c  o  m*/
 * @param xmlData
 * @return
 * @return
 * @return
 * @throws JAXBException
 */
@SuppressWarnings("unchecked")
public static <T> T xmlStringToPojo(String xmlData, Class<T> targetClass) throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(targetClass);
    StringReader reader = new StringReader(xmlData);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return (T) unmarshaller.unmarshal(reader);
}

From source file:de.bloxel.engine.util.JAXBUtils.java

public static <T> T unmarschal(final InputStream inputStream, final Class<T> aClass) {
    try {// w w w .j  a  v a2 s .co  m
        // http://jaxb.java.net/faq/index.html#classloader
        final JAXBContext jc = JAXBContext.newInstance(getPackageCanonicalName(aClass));
        final Unmarshaller unmarshaller = jc.createUnmarshaller();
        // http://jaxb.java.net/guide/Unmarshalling_is_not_working__Help_.html
        unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
        final XMLInputFactory staxFactory = XMLInputFactory.newInstance();
        final XMLStreamReader xmlReader = staxFactory.createXMLStreamReader(inputStream);
        return unmarshaller.unmarshal(xmlReader, aClass).getValue();
    } catch (final JAXBException e) {
        throw new RuntimeException(String.format("Can't load unmarschal '%s'", aClass), e);
    } catch (final XMLStreamException e) {
        throw new RuntimeException(String.format("Can't load unmarschal '%s'", aClass), e);
    }
}