public class PrettyPrinter extends DefaultHandler
DefaultHandler,
so to provide the unformatted XML just pass a new instance to a SAX parser.
Its output is via the toString method.
One major limitation: we gather character data for elements in a single
buffer, so mixed-content documents will lose a lot of data! This works
best with data-centric documents where elements either have single values
or child elements, but not both.| Modifier and Type | Class and Description |
|---|---|
static class |
PrettyPrinter.StreamAdapter |
| Constructor and Description |
|---|
PrettyPrinter() |
| Modifier and Type | Method and Description |
|---|---|
void |
characters(char[] chars,
int start,
int length)
When the
currentValue buffer is enabled, appends character
data into it, to be gathered when the element end tag is encountered. |
void |
endDocument()
Prints a blank line at the end of the reformatted document.
|
void |
endElement(String URI,
String name,
String qName)
Checks the
currentValue buffer to gather element content. |
static String |
prettyPrint(byte[] content)
Convenience method to wrap pretty-printing SAX pass over existing content.
|
static String |
prettyPrint(Document doc)
Convenience method to wrap pretty-printing SAX pass over existing content.
|
static String |
prettyPrint(InputStream content)
Convenience method to wrap pretty-printing SAX pass over existing content.
|
static String |
prettyPrint(String content)
Convenience method to wrap pretty-printing SAX pass over existing content.
|
void |
startDocument()
Prints the XML declaration.
|
void |
startElement(String URI,
String name,
String qName,
Attributes attributes)
Writes the start tag for the element.
|
String |
toString()
Call this to get the formatted XML post-parsing.
|
endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startPrefixMapping, unparsedEntityDecl, warningpublic static String prettyPrint(byte[] content)
public static String prettyPrint(String content)
public static String prettyPrint(InputStream content)
public static String prettyPrint(Document doc) throws TransformerException
TransformerExceptionpublic String toString()
public void startDocument()
throws SAXException
startDocument in interface ContentHandlerstartDocument in class DefaultHandlerSAXExceptionpublic void endDocument()
throws SAXException
endDocument in interface ContentHandlerendDocument in class DefaultHandlerSAXExceptionpublic void startElement(String URI, String name, String qName, Attributes attributes) throws SAXException
startElement in interface ContentHandlerstartElement in class DefaultHandlerSAXExceptionpublic void endElement(String URI, String name, String qName) throws SAXException
currentValue buffer to gather element content.
Writes this out if it is available. Writes the element end tag.endElement in interface ContentHandlerendElement in class DefaultHandlerSAXExceptionpublic void characters(char[] chars,
int start,
int length)
throws SAXException
currentValue buffer is enabled, appends character
data into it, to be gathered when the element end tag is encountered.characters in interface ContentHandlercharacters in class DefaultHandlerSAXExceptionCopyright © 2017. All rights reserved.