public static String getXML(final List<Event> events) {
        try {
            final TransformerHandler th = XMLUtil.createTransformerHandler(false);
            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            th.setResult(new StreamResult(outputStream));

            // Unwind all buffered events into the handler.
            final Deque<Event> openElements = new ArrayDeque<>();
            for (final Event event : events) {
                if (event.isStartElement()) {
                    openElements.push(event);
                } else if (event.isEndElement()) {
                    openElements.pop();
                }

                if (event.isEndDocument()) {
                    while (!openElements.isEmpty()) {
                        final StartElement startElement = (StartElement) openElements.pop();
                        th.endElement(startElement.getUri(), startElement.getLocalName(), startElement.getQName());
                    }

                    event.fire(th);
                } else {
                    event.fire(th);
                }
            }

            return outputStream.toString();

        } catch (final SAXException | TransformerConfigurationException | RuntimeException e) {
            throw ProcessException.create(e.getMessage());
        }
    }
Creates an XML string from the event stack captured for the matched
record.
stroom-pipeline/src/main/java/stroom/pipeline/xml/event/EventListUtils.java