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

booleanisSunCanonicalization(@Nonnull final Marshaller aMarshaller)
is Sun Canonicalization
final String sPropertyName = isInternalSunJAXB2Marshaller(aMarshaller) ? SUN_C14N_INTERNAL : SUN_C14N;
return _getBooleanProperty(aMarshaller, sPropertyName);
booleanisSunJAXB2Marshaller(@Nullable final Marshaller aMarshaller)
Check if the passed Marshaller is a Sun JAXB v2 marshaller.
if (aMarshaller == null)
    return false;
final String sClassName = aMarshaller.getClass().getName();
return sClassName.equals(JAXB_EXTERNAL_CLASS_NAME) ||
        sClassName.equals(JAXB_INTERNAL_CLASS_NAME);
voidmarshal(Class clz, T marshalObj)
marshal
JAXBContext context = JAXBContext.newInstance(clz);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(marshalObj, System.out);
Tmarshal(Class c, String xml)
marshal
T res;
if (c == xml.getClass()) {
    res = (T) xml;
else {
    JAXBContext ctx = JAXBContext.newInstance(c);
    Unmarshaller marshaller = ctx.createUnmarshaller();
    res = (T) marshaller.unmarshal(new StringReader(xml));
...
Tmarshal(Class c, String xml)
marshal
T res;
if (c == xml.getClass()) {
    res = (T) xml;
} else {
    JAXBContext ctx = JAXBContext.newInstance(c);
    Unmarshaller marshaller = ctx.createUnmarshaller();
    res = (T) marshaller.unmarshal(new StringReader(xml));
return res;
Stringmarshal(Class clazz, T obj)
marshal
StringWriter out = new StringWriter();
JAXBContext context = JAXBContext.newInstance(clazz);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(obj, out);
return out.toString();
Stringmarshal(Class clazz, T object)
marshal
StringWriter writer = new StringWriter();
JAXBContext jc = JAXBContext.newInstance(clazz);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(new JAXBElement<T>(new QName("body"), clazz, object), writer);
String actual = writer.toString();
return actual;
Stringmarshal(final Object obj, final Class... classes)
JAXBContext.newInstance(..) is a slow operation.
return marshal(JAXBContext.newInstance(classes), obj);
Stringmarshal(final Object object)
marshal
try {
    final JAXBContext context = JAXBContext.newInstance(object.getClass());
    final Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    final StringWriter out = new StringWriter();
    marshaller.marshal(object, out);
    return out.toString();
} catch (JAXBException e) {
...
Stringmarshal(final Object object)
marshal
Gson gson = new Gson();
return gson.toJson(object);