Example usage for javax.xml.bind JAXBContext newInstance

List of usage examples for javax.xml.bind JAXBContext newInstance

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext newInstance.

Prototype

public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException 

Source Link

Document

Create a new instance of a JAXBContext class.

Usage

From source file:Main.java

private static Unmarshaller getJaxbUnmarshaller(Class<?> classesToBeBound,
        Map<String, Object> unmarshallerProps) throws Exception {
    Unmarshaller unmarshaller = JAXB_UNMARSHALLER_CACHE.get(classesToBeBound);
    if (unmarshaller == null) {
        JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound);
        unmarshaller = jaxbContext.createUnmarshaller();
        if (unmarshallerProps != null && unmarshallerProps.size() > 0) {
            for (Map.Entry<String, Object> prop : unmarshallerProps.entrySet()) {
                unmarshaller.setProperty(prop.getKey(), prop.getValue());
            }/*from w  w w .  j a  va 2s . c  o  m*/
        }
        JAXB_UNMARSHALLER_CACHE.put(classesToBeBound, unmarshaller);
    }
    return unmarshaller;
}

From source file:Main.java

public static Object xmlStrToObj(String inputStr, Class inputClass) throws Exception {
    byte[] byteArray = inputStr.getBytes();
    ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);
    XMLInputFactory input = XMLInputFactory.newFactory();
    XMLStreamReader reader = input.createXMLStreamReader(byteStream);

    JAXBContext context = JAXBContext.newInstance(inputClass);
    Unmarshaller unmarsh = context.createUnmarshaller();
    Object result = unmarsh.unmarshal(reader);

    return result;
}

From source file:Main.java

public static <T> T unMarshal(String xml, Class<T> clazz, String encoding) {
    T result = null;/* w  w w .ja va  2  s.  c  om*/
    try {
        ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
        JAXBContext context = JAXBContext.newInstance(clazz);
        result = (T) context.createUnmarshaller()
                .unmarshal(new InputSource(new InputStreamReader(is, encoding)));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return result;

}

From source file:Main.java

public static void marshal(JAXBContext context, Object object, Writer writer, Map<String, Object> properties)
        throws JAXBException {
    if (object == null) {
        return;/*from ww  w.j a v  a2  s  .c o  m*/
    }

    if (writer == null) {
        return;
    }

    if (context == null) {
        context = JAXBContext.newInstance(object.getClass());
    }

    Marshaller marshaller = context.createMarshaller();
    if (properties != null) {
        Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            marshaller.setProperty(entry.getKey(), entry.getValue());
        }
    }
    marshaller.marshal(object, writer);
}

From source file:Main.java

public static <T> T unserializer(Class<T> clazz, byte[] xml) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream is = new ByteArrayInputStream(xml);
    @SuppressWarnings("unchecked")
    T obj = (T) unmarshaller.unmarshal(is);
    return obj;/*www .  ja va 2 s  .c  om*/
}

From source file:Main.java

/**
 * this method is responsible for converting any pojo to respective xml form
 * //from   ww  w .java 2s . com
 * @param object
 * @param filePath
 * @throws JAXBException
 * @throws IOException
 */
public static File pojoToXml(Object object, String filePath) throws JAXBException, IOException {

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    OutputStream os = new FileOutputStream(filePath);
    marshaller.marshal(object, os);
    File file = new File(filePath);
    return file;
}

From source file:Product.java

public static Product fromXML(InputStream in) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Product.class);
    Unmarshaller um = context.createUnmarshaller();
    return (Product) um.unmarshal(in);
}

From source file:Main.java

private static Marshaller createMarshaller(Object o) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    return jaxbMarshaller;
}

From source file:Main.java

public static <T> T UnMarshall(Class<T> objectClass, Node node) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(objectClass);
    Unmarshaller u = jc.createUnmarshaller();
    return objectClass.cast(u.unmarshal(node));
}

From source file:Main.java

/**
 * Returns the corresponding {@link JAXBContext} for the given {@link Class}.
 * //  w  w  w  .  ja  v  a 2s. c om
 * @param clazz {@link Class}
 * @return {@link JAXBContext}
 * @throws JAXBException
 */
private static <T> JAXBContext getContext(Class<T> clazz) throws JAXBException {
    synchronized (contextCache) {
        if (!contextCache.containsKey(clazz)) {
            contextCache.put(clazz, JAXBContext.newInstance(clazz));
        }
    }

    return contextCache.get(clazz);
}