@Override
    public void endElement(final String uri, final String localName, final String qName) throws SAXException {
        if (depth > 2) {
            handler.endElement(uri, localName, qName);

        } else if (depth == 2) {
            handler.endElement(uri, localName, qName);

            // Get the body
            body = getBody();

            handler.endElement(root.getUri(), root.getLocalName(), root.getQName());
            for (int i = prefixes.length - 1; i >= 0; i--) {
                final StartPrefixMapping prefixMapping = prefixes[i];
                handler.endPrefixMapping(prefixMapping.getPrefix());
            }
            handler.endDocument();

            // Get the footer
            footer = getFooter();

            final ByteSlice headerBytes = StreamUtil.getByteSlice(header);
            final ByteSlice footerBytes = StreamUtil.getByteSlice(footer);
            final ByteSlice bodyBytes = StreamUtil.getByteSlice(body);

            final Rec record = new Rec(headerBytes, footerBytes, bodyBytes);

            /// Reset
            header = null;
            footer = null;
            body = null;
            handler = null;
        }

        depth--;
    }
Don't end prefix mappings if we are at the root element. Leave the end
processing method to do this.
stroom-pipeline/src/main/java/stroom/pipeline/record/XMLRecordEmitter.java