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(JAXBContext context, Object object, Writer writer, Map properties)
marshal
if (object == null) {
    return;
if (writer == null) {
    return;
if (context == null) {
    context = JAXBContext.newInstance(object.getClass());
...
voidmarshal(JAXBElement e, File f)
Marshals the given element to file.
mar.marshal(e, f);
Elementmarshal(JAXBElement jaxbElement, Class cls)
marshal
try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    JAXBContext jaxbContext = JAXBContext.newInstance(cls);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.marshal(jaxbElement, doc);
...
voidmarshal(JAXBElement value, String contextPath, OutputStream out, ClassLoader classLoader)
Kirjoitetaan XML-dataa.
JAXBContext jc = JAXBContext.newInstance(contextPath, classLoader);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(value, out);
voidmarshal(Marshaller m, File out, Object o)
Stores object into file.
try (FileOutputStream fos = new FileOutputStream(out)) {
    m.marshal(o, fos);
} catch (JAXBException ex) {
    throw new IOException("Cannot write object to file - " + ex.getMessage(), ex);
voidmarshal(Marshaller marshaller, Object object, String filename)
marshal
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
FileOutputStream fileOutputStream = null;
try {
    fileOutputStream = new FileOutputStream(filename);
    marshaller.marshal(object, fileOutputStream);
} finally {
    if (fileOutputStream != null) {
        try {
...
voidmarshal(Marshaller marshaller, Object object, String filename)
marshal
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
FileOutputStream fileOutputStream = null;
try {
    fileOutputStream = new FileOutputStream(filename);
    marshaller.marshal(object, fileOutputStream);
} finally {
    if (fileOutputStream != null) {
        try {
...
Stringmarshal(Object bean)
marshal
StringWriter writer = new StringWriter();
getMarshaller().marshal(bean, writer);
return writer.toString();
byte[]marshal(Object entity)

Marshal an object into a byte array.

return marshal(entity, ImmutableMap.<String, Object>of(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE,
        Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.toString()));
DOMResultmarshal(Object obj)
marshal
DOMResult domResult = new DOMResult();
try {
    getMarshaller().marshal(obj, domResult);
} catch (Exception e) {
    throw new RuntimeException("Cannot serialize object: " + obj, e);
return domResult;