Java Code Examples for javax.xml.bind.Marshaller.setAdapter()

The following are Jave code examples for showing how to use setAdapter() of the javax.xml.bind.Marshaller class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: stroom-stats   File: XMLMarshallerUtil.java   Source Code and License Vote up 6 votes
public static <T> String marshal(final JAXBContext context, final T obj, final XmlAdapter<?, ?>... adapters) {
    if (obj == null) {
        return null;
    }

    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final Marshaller marshaller = context.createMarshaller();

        if (adapters != null) {
            for (final XmlAdapter<?, ?> adapter : adapters) {
                marshaller.setAdapter(adapter);
            }
        }

        final TransformerHandler transformerHandler = XMLUtil.createTransformerHandler(true);
        transformerHandler.setResult(new StreamResult(out));
        marshaller.marshal(obj, transformerHandler);

        return out.toString(String.valueOf(StandardCharsets.UTF_8));
    } catch (final Throwable t) {
        throw new RuntimeException(t.getMessage(), t);
    }
}