public static void copyXMLStream(
            final XMLStreamReader reader,
            final XMLStreamWriter writer,
            final StringBuilder textBuilder,
            final int flags,
            final boolean indentParentElementText,
            final String parentElement) throws XMLStreamException {
        int depth = 0;
        // IMPORTANT: we explicitly ignore the START_DOCUMENT and END_DOCUMENT
        // events; we don't want to accidentally close the destination stream,
        // since the caller may still be using it.
        for (int eventType = reader.getEventType();; eventType = reader.next()) {
            switch (eventType) {
            case XMLStreamConstants.ATTRIBUTE:
            case XMLStreamConstants.DTD:
            case XMLStreamConstants.NAMESPACE:
            case XMLStreamConstants.NOTATION_DECLARATION:
            case XMLStreamConstants.ENTITY_DECLARATION: {
                // ignored
                break;
            }
            case XMLStreamConstants.START_DOCUMENT: {
                if (writer != null && (flags & COPY_START_DOCUMENT) != 0) {
                    writer.writeStartDocument();
                }
                break;
            }
            case XMLStreamConstants.END_DOCUMENT: {
                if (writer != null && (flags & COPY_END_DOCUMENT) != 0) {
                    writer.writeEndDocument();
                }
                return;
            }
            case XMLStreamConstants.START_ELEMENT: {
                depth++;
                if (writer != null) {
                    copyStartElement(reader, writer);
                }
                if (textBuilder != null) {
                    final String name = reader.getLocalName();
                    if (name.equals("document")) {
                        textBuilder.append("   ");
                    }
                    textBuilder.append('<').append(name).append('>');
                }
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                if (writer != null) {
                    writer.writeEndElement();
                }
                if (textBuilder != null) {
                    final String name = reader.getLocalName();
                    if (name.equals("document")) {
                        textBuilder.append("   ");
                    }
                    textBuilder.append("</").append(name).append('>');
                }
                if (--depth <= 0) {
                    if ((flags & COPY_SINGLE_ELEMENT) != 0) {
                        return;
                    }
                }
                break;
            }
            case XMLStreamConstants.PROCESSING_INSTRUCTION: {
                if (writer != null) {
                    writer.writeProcessingInstruction(reader.getPITarget(), reader.getPIData());
                }
                if (textBuilder != null) {
                    textBuilder.append(reader.getText());
                }
                break;
            }
            case XMLStreamConstants.CHARACTERS: {
                final String text = reader.getText();
                if (writer != null) {
                    writer.writeCharacters(text);
                }
                if (textBuilder != null) {
                    textBuilder.append(text);
                }
                break;
            }
            case XMLStreamConstants.COMMENT: {
                final String text = reader.getText();
                if (writer != null && (flags & COPY_COMMENTS) != 0) {
                    writer.writeComment(text);
                }
                if (textBuilder != null && (flags & COPY_COMMENTS) != 0) {
                    textBuilder.append(text);
                }
                break;
            }
            case XMLStreamConstants.SPACE: {
                final String text = reader.getText();
                if (writer != null && (flags & COPY_IGNORABLE_WHITESPACE) != 0) {
                    writer.writeCharacters(text);
                }
                if (textBuilder != null && (flags & COPY_IGNORABLE_WHITESPACE) != 0) {
                    textBuilder.append(text);
                }
                break;
            }
            case XMLStreamConstants.ENTITY_REFERENCE: {
                final String text = reader.getText();
                if (writer != null) {
                    writer.writeEntityRef(text);
                }
                if (textBuilder != null) {
                    textBuilder.append(text);
                }
                break;
            }
            case XMLStreamConstants.CDATA: {
                final String text = reader.getText();
                if (writer != null) {
                    writer.writeCData(text);
                }
                if (textBuilder != null) {
                    textBuilder.append(text);
                }
            }
            }
        }
    }
public static void copyXMLStream ( final XMLStreamReader reader , final XMLStreamWriter writer , final StringBuilder textBuilder , final int flags , final boolean indentParentElementText , final String parentElement ) throws XMLStreamException { int depth = 0 ; for ( int eventType = reader . getEventType ( ) ; ; eventType = reader . next ( ) ) { switch ( eventType ) { case XMLStreamConstants . ATTRIBUTE : case XMLStreamConstants . DTD : case XMLStreamConstants . NAMESPACE : case XMLStreamConstants . NOTATION_DECLARATION : case XMLStreamConstants . ENTITY_DECLARATION : { break ; } case XMLStreamConstants . START_DOCUMENT : { if ( writer != null && ( flags & COPY_START_DOCUMENT ) != 0 ) { writer . writeStartDocument ( ) ; } break ; } case XMLStreamConstants . END_DOCUMENT : { if ( writer != null && ( flags & COPY_END_DOCUMENT ) != 0 ) { writer . writeEndDocument ( ) ; } return ; } case XMLStreamConstants . START_ELEMENT : { depth ++ ; if ( writer != null ) { copyStartElement ( reader , writer ) ; } if ( textBuilder != null ) { final String name = reader . getLocalName ( ) ; if ( name . equals ( "document" ) ) { textBuilder . append ( "   " ) ; } textBuilder . append ( '<' ) . append ( name ) . append ( '>' ) ; } break ; } case XMLStreamConstants . END_ELEMENT : { if ( writer != null ) { writer . writeEndElement ( ) ; } if ( textBuilder != null ) { final String name = reader . getLocalName ( ) ; if ( name . equals ( "document" ) ) { textBuilder . append ( "   " ) ; } textBuilder . append ( "</" ) . append ( name ) . append ( '>' ) ; } if ( -- depth <= 0 ) { if ( ( flags & COPY_SINGLE_ELEMENT ) != 0 ) { return ; } } break ; } case XMLStreamConstants . PROCESSING_INSTRUCTION : { if ( writer != null ) { writer . writeProcessingInstruction ( reader . getPITarget ( ) , reader . getPIData ( ) ) ; } if ( textBuilder != null ) { textBuilder . append ( reader . getText ( ) ) ; } break ; } case XMLStreamConstants . CHARACTERS : { final String text = reader . getText ( ) ; if ( writer != null ) { writer . writeCharacters ( text ) ; } if ( textBuilder != null ) { textBuilder . append ( text ) ; } break ; } case XMLStreamConstants . COMMENT : { final String text = reader . getText ( ) ; if ( writer != null && ( flags & COPY_COMMENTS ) != 0 ) { writer . writeComment ( text ) ; } if ( textBuilder != null && ( flags & COPY_COMMENTS ) != 0 ) { textBuilder . append ( text ) ; } break ; } case XMLStreamConstants . SPACE : { final String text = reader . getText ( ) ; if ( writer != null && ( flags & COPY_IGNORABLE_WHITESPACE ) != 0 ) { writer . writeCharacters ( text ) ; } if ( textBuilder != null && ( flags & COPY_IGNORABLE_WHITESPACE ) != 0 ) { textBuilder . append ( text ) ; } break ; } case XMLStreamConstants . ENTITY_REFERENCE : { final String text = reader . getText ( ) ; if ( writer != null ) { writer . writeEntityRef ( text ) ; } if ( textBuilder != null ) { textBuilder . append ( text ) ; } break ; } case XMLStreamConstants . CDATA : { final String text = reader . getText ( ) ; if ( writer != null ) { writer . writeCData ( text ) ; } if ( textBuilder != null ) { textBuilder . append ( text ) ; } } } } }
Copy the XML input stream to the XML Writer as well as to a StringBuilder.

@param reader
the XMLStreamReader
@param writer
the XMLStreamWriter
@param textBuilder
the StringBuilder
@param flags
the flags indicating copying options
@param indentParentElementText
a boolean flag indicating if what to indent before the parent element when copying
to the StringBuilder
@param parentElement
the parent element
@throws XMLStreamException
if there is error processing stream