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 String toString(Object entity, boolean formatOutput) {

    StringWriter stringWriter = new StringWriter();

    try {/*from   w ww .  j av a 2  s  .  com*/

        Map<String, Object> properties = new HashMap<String, Object>();

        properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, formatOutput);

        JAXBContext jaxbContext = JAXBContext.newInstance(entity.getClass());

        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.marshal(entity, stringWriter);

    } catch (JAXBException e) {
        stringWriter.write(e.getMessage());
    }
    return stringWriter.toString();
}

From source file:Main.java

/**
 * @param <T>/*  w w  w .j a  v  a 2 s.  c o m*/
 *           the type to serialize
 * @param c
 *           the class of the type to serialize
 * @param o
 *           the instance containing the data to serialize
 * @return a string representation of the data.
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static <T> String marshal(Class<T> c, Object o) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(c);
    Marshaller marshaller = ctx.createMarshaller();
    StringWriter entityXml = new StringWriter();
    marshaller.marshal(o, entityXml);
    String entityString = entityXml.toString();
    return entityString;
}

From source file:Main.java

public static String convertBean2Xml(Object obj) throws IOException {
    String result = null;//from w  ww.  j  ava2s  . c o m
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos,
                (String) marshaller.getProperty(Marshaller.JAXB_ENCODING));
        xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
        marshaller.marshal(obj, xmlStreamWriter);
        xmlStreamWriter.writeEndDocument();
        xmlStreamWriter.close();
        result = baos.toString("UTF-8");

    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    } catch (XMLStreamException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
    return result;
}

From source file:Main.java

private static Unmarshaller createUmarshall(String pkgName) throws JAXBException, SAXException {
    JAXBContext jaxbCtx = null;//from  w  ww  .j  a va2 s . c  o m
    if ((jaxbCtx = marshallContexts.get(pkgName)) == null) {
        jaxbCtx = JAXBContext.newInstance(pkgName);
        marshallContexts.put(pkgName, jaxbCtx);
    }
    Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();

    return unmarshaller;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshallXml(Class<T> clazz, InputStream in)
        throws JAXBException, UnsupportedEncodingException {

    String className = clazz.getPackage().getName();
    JAXBContext context = JAXBContext.newInstance(className);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    //Reader reader = new IgnoreIllegalCharactersXmlReader(in);

    Reader reader = new InputStreamReader(in, "UTF-8");

    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");

    Object result = unmarshaller.unmarshal(is); //file);
    return (T) result;
}

From source file:Main.java

public static <T> T deserializeJaxb(Class<T> cls, Node node) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(cls);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return (T) unmarshaller.unmarshal(node);
}

From source file:Main.java

private static Marshaller getJaxbMarshaller(Class<?> classesToBeBound, Map<String, Object> marshallerProps)
        throws Exception {
    Marshaller marshaller = JAXB_MARSHALLER_CACHE.get(classesToBeBound);
    if (marshaller == null) {
        JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound);
        marshaller = jaxbContext.createMarshaller();
        if (marshallerProps != null && marshallerProps.size() > 0) {
            for (Map.Entry<String, Object> prop : marshallerProps.entrySet()) {
                marshaller.setProperty(prop.getKey(), prop.getValue());
            }/* ww  w. j  a v a2s. co  m*/
        }
        JAXB_MARSHALLER_CACHE.put(classesToBeBound, marshaller);
    }
    return marshaller;
}

From source file:Main.java

public static Object xmlTobean(String path, String classPath) {
    Object obj = null;/*from  www  . j a v  a 2 s.  c om*/
    Class<?> obClass = null;
    try {
        obClass = Class.forName(classPath);
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        File file = new File(path);
        JAXBContext context = JAXBContext.newInstance(obClass);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        obj = unmarshaller.unmarshal(file);
        System.out.println(obj);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

/**
 * Serializes any Object to XML// ww w  .ja  v  a 2s  .  c o  m
 *
 * @param object Object to serialize
 * @return XML serialization of the supplied object
 */
public static String toXmlJaxb(Object object) {
    String result = "";
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        result = writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

public static Object xmlToJaxb(Class<?> xmlClass, String xml) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    JAXBElement<?> element;
    try (StringReader reader = new StringReader(xml)) {
        element = (JAXBElement<?>) unmarshaller.unmarshal(reader);
    }/*www.j a  v a 2s . c  om*/
    return element.getValue();
}