java.lang.Objectorg.xml.sax.helpers.XMLFilterImpl
sax.XMLFilterBase
sax.DataFormatFilter
All Implemented Interfaces:
LexicalHandler, ErrorHandler, EntityResolver, XMLFilter, ContentHandler, DTDHandler
This filter adds indentation and newlines to field-oriented XML without mixed content. All added indentation and newlines will be passed on down the filter chain.
In general, all whitespace in an XML document is potentially significant. There is, however, a large class of XML documents where information is strictly fielded: each element contains either character data or other elements, but not both. For this special case, it is possible for a filter to provide automatic indentation and newlines. Note that this class will likely not yield appropriate results for document-oriented XML like XHTML pages, which mix character data and elements together.
This filter will automatically place each start tag on a new line, optionally indented if an indent step is provided (by default, there is no indentation). If an element contains other elements, the end tag will also appear on a new line with leading indentation. Consider, for example, the following code:
DataFormatFilter df = new DataFormatFilter();
df.setContentHandler(new XMLWriter());
df.setIndentStep(2);
df.startDocument();
df.startElement("Person");
df.dataElement("name", "Jane Smith");
df.dataElement("date-of-birth", "1965-05-23");
df.dataElement("citizenship", "US");
df.endElement("Person");
df.endDocument();
This code will produce the following document:
<?xml version="1.0"?> <Person> <name>Jane Smith</name> <date-of-birth>1965-05-23</date-of-birth> <citizenship>US</citizenship> </Person>
| Fields inherited from sax.XMLFilterBase: |
|---|
| EMPTY_ATTS, LEXICAL_HANDLER_NAMES |
| Constructor: |
|---|
|
Use the XMLReader provided as the source of events.
|
| Method from sax.DataFormatFilter Summary: |
|---|
| characters, endElement, getIndentStep, reset, setIndentStep, startDocument, startElement |
| Methods from sax.XMLFilterBase: |
|---|
| characters, comment, dataElement, dataElement, dataElement, dataElement, emptyElement, emptyElement, emptyElement, emptyElement, endCDATA, endDTD, endElement, endElement, endEntity, getLexicalHandler, getProperty, parse, setLexicalHandler, setProperty, startCDATA, startDTD, startElement, startElement, startElement, startEntity |
| Methods from org.xml.sax.helpers.XMLFilterImpl: |
|---|
| characters, endDocument, endElement, endPrefixMapping, error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, getProperty, ignorableWhitespace, notationDecl, parse, parse, processingInstruction, resolveEntity, setContentHandler, setDTDHandler, setDocumentLocator, setEntityResolver, setErrorHandler, setFeature, setParent, setProperty, skippedEntity, startDocument, startElement, startPrefixMapping, unparsedEntityDecl, warning |
| Methods from java.lang.Object: |
|---|
| equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from sax.DataFormatFilter Detail: |
|---|
|
If the element has contained other elements, the tag will appear indented on a new line; otherwise, it will appear immediately following whatever came before. The newline and indentation will be passed on down the filter chain through regular characters events. |
Return the current indent step: each start tag will be indented by this number of spaces times the number of ancestors that the element has. |
This method is especially useful if the filter failed with an exception the last time through. |
|
Reset state and pass the event on for further processing. |
Each tag will begin on a new line, and will be indented by the current indent step times the number of ancestors that the element has. The newline and indentation will be passed on down the filter chain through regular characters events. |