Java Utililty Methods XML JAXB Marshaller

List of utility methods to do XML JAXB Marshaller

Description

The list of methods to do XML JAXB Marshaller are organized into topic(s).

Method

voidmarshal(Object object, OutputStream stream)
Marshal an arbitrary object with JAXB.
JAXB.marshal(object, stream);
stream.close();
voidmarshal(Object objectToMarshal)
Marshal the provided object using System.out
marshal(objectToMarshal, System.out);
Stringmarshal(Object source, Class configurationClass)
marshal
JAXBContext jAXBContext = JAXBContext.newInstance(configurationClass);
StringWriter writer = new StringWriter();
jAXBContext.createMarshaller().marshal(source, writer);
return writer.toString();
Stringmarshal(T bean, Class... bc)
Convert a bean to XML format using JAXB.
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
...
Stringmarshal(T clazz)
Marshal an entity class into a XML String representation.
JAXBContext jaxbContext = JAXBContext.newInstance(clazz.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter stringWriter = new StringWriter();
marshaller.marshal(clazz, stringWriter);
return stringWriter.toString();
Stringmarshal(T object)
marshal
Class<T> clzz = (Class<T>) object.getClass();
JAXBContext context;
context = JAXBContext.newInstance(clzz);
Marshaller m = context.createMarshaller();
ByteArrayOutputStream os = new ByteArrayOutputStream();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(object, os);
return os.toString();
...
Stringmarshal(T t, Class entityClass)
marshal
StringWriter sw = new StringWriter();
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(entityClass);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(t, sw);
} catch (JAXBException e) {
...
StringmarshalAsString(Class clz, T marshalObj)
marshal As String
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();
Tmarshall(Class c, String xml)
marshall
T res;
if (c == xml.getClass()) {
    res = (T) xml;
} else {
    JAXBContext ctx = JAXBContext.newInstance(c);
    Unmarshaller unmarshaller = ctx.createUnmarshaller();
    res = (T) unmarshaller.unmarshal(new StringReader(xml));
return res;
Stringmarshall(final Object o, Class clazz)
Marshalls the given JaxB class into a XML document while explicitly providing the class
Marshaller m;
try {
    m = JAXBContext.newInstance(clazz).createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    final StringWriter w = new StringWriter();
    m.marshal(o, w);
    return w.toString();
} catch (JAXBException e) {
...