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

public static void saveInstance(Writer output, Object instance)

        throws JAXBException, IOException {

    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING);

    marshaller.marshal(instance, output);

    output.flush();//from w w w. j  av a2s .c o  m

}

From source file:Main.java

public static final String obj2xml(final Object obj, final boolean formatted) throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext contextOut = JAXBContext.newInstance(obj.getClass());
    Marshaller marshallerOut = contextOut.createMarshaller();
    marshallerOut.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
    marshallerOut.marshal(obj, writer);/*from  www . j a va 2  s  .  c o  m*/

    return new String(writer.getBuffer());
}

From source file:Main.java

/**
 * Returns a cached <i>JAXB Context</i>.
 *
 * @since 3.8/* w w  w  .j  a  v a 2s .c o m*/
 */
public synchronized static JAXBContext createContext(final Class<?> bind) throws JAXBException {
    JAXBContext context = null;
    SoftReference<JAXBContext> ref = contextCache.get(bind);
    if (ref != null) {
        context = ref.get();
        if (context == null)
            contextCache.remove(bind);
    }

    if (context != null)
        return context;

    synchronized (bind) {
        synchronized (JAXBContext.class) {
            context = JAXBContext.newInstance(bind);
        }
    }

    contextCache.put(bind, new SoftReference<JAXBContext>(context));

    return context;
}

From source file:Main.java

public static void marshal(Object o, OutputStream os, Map<String, Object> properties) {
    try {/*from  w w w. j  a v a2s.  c  o  m*/
        JAXBContext ctx = JAXBContext.newInstance(o.getClass());
        Marshaller marshaller = ctx.createMarshaller();

        for (Entry<String, Object> p : properties.entrySet()) {
            marshaller.setProperty(p.getKey(), p.getValue());
        }

        marshaller.marshal(o, os);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> Object xmlToObject(InputStream xmlContent, Class<T> clazz) throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setSchema(null); //note: setting schema to null will turn validator off
    Object unMarshalled = unmarshaller.unmarshal(xmlContent);
    Object xmlObject = clazz.cast(unMarshalled);

    return (T) xmlObject;
}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * /*from w ww . j ava2 s.  c  o m*/
 * @param bean
 *            The bean to marshal as XML.
 * @param bc
 *            Additional classes to register on the JAXB context for
 *            marshalling.
 * @return An XML representation of the bean.
 */
public static <T> String marshal(T bean, Class<?>... bc) {
    assert bean != null;
    Class<?>[] bind;
    if (bc.length > 0) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = bean.getClass();
        for (int i = 0; i < bc.length; i++)
            bind[i + 1] = bc[i];
    } else
        bind = new Class<?>[] { bean.getClass(), };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        marshaller.marshal(bean, baos);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error marshalling", e);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * /*w w w  . ja v a  2s . c  o  m*/
 * @param bean
 *            The bean to marshal as XML.
 * @param bc
 *            Additional classes to register on the JAXB context for
 *            marshalling.
 * @return An XML representation of the bean.
 */
public static <T> String marshal(T bean, Class<?>... bc) {
    assert bean != null;
    Class<?>[] bind;
    if (bc.length > 0) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = bean.getClass();
        for (int i = 0; i < bc.length; i++)
            bind[i + 1] = bc[i];
    } else
        bind = new Class<?>[] { bean.getClass(), };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(bean, baos);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error marshalling", e);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

public static <T extends Object> String marshalAsString(Class clz, T marshalObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clz);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter writer = new StringWriter();
    marshaller.marshal(marshalObj, new BufferedWriter(writer));
    return writer.toString();

}

From source file:Main.java

public static <T> T unserializer(Class<T> clazz, InputStream xmlInput) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    @SuppressWarnings("unchecked")
    T obj = (T) unmarshaller.unmarshal(xmlInput);
    return obj;/*  w  w  w  .  ja  va 2s  .  c  o m*/
}

From source file:Main.java

public static JAXBContext getJAXBContext(Class<?> c) throws JAXBException {
    JAXBContext context = jaxbcontextmap.get(c.getName());
    if (context != null) {
        return context;
    } else {/* w w  w  . j  a v  a 2 s. co  m*/
        context = JAXBContext.newInstance(c);
        jaxbcontextmap.put(c.getName(), context);
        return context;
    }
}