Example usage for javax.xml.bind JAXBContext createMarshaller

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

Introduction

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

Prototype

public abstract Marshaller createMarshaller() throws JAXBException;

Source Link

Document

Create a Marshaller object that can be used to convert a java content tree into XML data.

Usage

From source file:Main.java

public static String asXml(Object object) throws IOException {
    try {/*from www.j ava2  s  . c  o  m*/
        StringWriter writer = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(object.getClass());
        Marshaller m = context.createMarshaller();
        m.marshal(object, writer);

        return writer.toString();

    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static void serialize(Object o, OutputStream os, Boolean format) throws JAXBException {
    String pkg = o.getClass().getPackage().getName();
    JAXBContext jc = getCachedContext(pkg);
    Marshaller m = jc.createMarshaller();
    m.setEventHandler(new DefaultValidationEventHandler());
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    m.marshal(o, os);/* w  w  w  . j a v  a2  s.c  o  m*/
}

From source file:Main.java

public static String marshallToStringNoValidation(final Object xmlObject) throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(xmlObject.getClass());
    Marshaller jaxbMarshaller = context.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(xmlObject, writer);
    return writer.toString();
}

From source file:Main.java

public static Document deserialize(Object object) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//w  w w .ja  v a 2 s.  c o  m
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(object, doc);
    return doc;
}

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());
            }//from  www. j  ava 2  s. co  m
        }
        JAXB_MARSHALLER_CACHE.put(classesToBeBound, marshaller);
    }
    return marshaller;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static String ObjToXml(Object object, boolean isXmlFormat, Class... classesToBeBound)
        throws JAXBException, IOException {
    JAXBContext context = JAXBContext.newInstance(classesToBeBound);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isXmlFormat);
    m.setSchema(null);/*from w ww. ja  v  a 2  s .com*/
    StringWriter sw = new StringWriter();
    m.marshal(object, sw);
    String result = sw.toString();
    sw.close();
    return result;
}

From source file:Main.java

public static void Object2XmlFile(Object ob, String path) throws JAXBException, FileNotFoundException {
    Class local = ob.getClass();/* w  ww  .  jav  a 2  s .  co  m*/
    JAXBContext context = JAXBContext.newInstance(new Class[] { local });
    Marshaller marshaller = context.createMarshaller();
    // marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
    // paramNamespacePrefixMapper);
    // marshaller.setProperty("jaxb.formatted.output",
    // Boolean.valueOf(true));
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(ob, new FileOutputStream(new File(path)));
}

From source file:Main.java

public static String beanToXml(Object to) {
    try {/*from www.  j av  a  2 s .  c om*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        JAXBContext context = JAXBContext.newInstance(to.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(to, out);
        return new String(out.toByteArray(), "UTF-8");
    } catch (JAXBException | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String beanToXml(ByteArrayOutputStream out, Object to) {
    try {//  w w  w . j a  v  a2 s . com
        JAXBContext context = JAXBContext.newInstance(to.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(to, out);
        return new String(out.toByteArray(), "UTF-8");
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

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();

}