Java tutorial
//package com.java2s; //License from project: Apache License import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import java.io.StringWriter; import java.io.Writer; import java.util.*; public class Main { public static void marshal(JAXBContext context, Object object, Writer writer, Map<String, Object> properties) throws JAXBException { if (object == null) { return; } if (writer == null) { return; } if (context == null) { context = JAXBContext.newInstance(object.getClass()); } Marshaller marshaller = context.createMarshaller(); if (properties != null) { Iterator<Map.Entry<String, Object>> it = properties.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Object> entry = it.next(); marshaller.setProperty(entry.getKey(), entry.getValue()); } } marshaller.marshal(object, writer); } public static String marshal(JAXBContext context, Object object, Map<String, Object> properties) throws JAXBException { StringWriter writer = new StringWriter(); marshal(context, object, writer, properties); return writer.toString(); } public static String marshal(Object object) throws JAXBException { StringWriter writer = new StringWriter(); marshal(null, object, writer, null); return writer.toString(); } }