Class IndentingXMLEventWriter

  • All Implemented Interfaces:
    javax.xml.stream.util.XMLEventConsumer, javax.xml.stream.XMLEventWriter, Indentation

    public class IndentingXMLEventWriter
    extends EventWriterDelegate
    implements Indentation
    A filter that indents an XML stream. To apply it, construct a filter that contains another XMLEventWriter, which you pass to the constructor. Then call methods of the filter instead of the contained stream. For example:
     XMLEventWriter stream = ...
     stream = new IndentingXMLEventWriter(stream);
     stream.add(...);
     
    The filter inserts characters to format the document as an outline, with nested elements indented. Basically, it inserts a line break and whitespace before:
    • each DTD, processing instruction or comment that's not preceded by data
    • each starting tag that's not preceded by data
    • each ending tag that's preceded by nested elements but not data
    This works well with 'data-oriented' XML, wherein each element contains either data or nested elements but not both. It can work badly with other styles of XML. For example, the data in a 'mixed content' document are apt to be polluted with indentation characters.

    Indentation can be adjusted by setting the newLine and indent properties. But set them to whitespace only, for best results. Non-whitespace is apt to cause problems, for example when this class attempts to insert newLine before the root element.

    • Constructor Detail

      • IndentingXMLEventWriter

        public IndentingXMLEventWriter​(javax.xml.stream.XMLEventWriter out)
    • Method Detail

      • getIndent

        public java.lang.String getIndent()
        Description copied from interface: Indentation
        The characters used for one level of indentation.
        Specified by:
        getIndent in interface Indentation
        Returns:
        the indentation string
      • setIndent

        public void setIndent​(java.lang.String indent)
        Description copied from interface: Indentation
        Set the characters used for one level of indentation. The default is Indentation.DEFAULT_INDENT. "\t" is a popular alternative.
        Specified by:
        setIndent in interface Indentation
        Parameters:
        indent - the indentation string
      • add

        public void add​(javax.xml.stream.events.XMLEvent event)
                 throws javax.xml.stream.XMLStreamException
        Specified by:
        add in interface javax.xml.stream.util.XMLEventConsumer
        Specified by:
        add in interface javax.xml.stream.XMLEventWriter
        Overrides:
        add in class EventWriterDelegate
        Throws:
        javax.xml.stream.XMLStreamException
      • beforeMarkup

        protected void beforeMarkup()
                             throws javax.xml.stream.XMLStreamException
        Throws:
        javax.xml.stream.XMLStreamException
      • afterMarkup

        protected void afterMarkup()
      • afterData

        protected void afterData()
      • beforeStartElement

        protected void beforeStartElement()
                                   throws javax.xml.stream.XMLStreamException
        Throws:
        javax.xml.stream.XMLStreamException
      • afterStartElement

        protected void afterStartElement()
      • beforeEndElement

        protected void beforeEndElement()
      • afterEndElement

        protected void afterEndElement()
      • afterEndDocument

        protected void afterEndDocument()
                                 throws javax.xml.stream.XMLStreamException
        Throws:
        javax.xml.stream.XMLStreamException