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, warning
public 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
TransformerException
public String toString()
public void startDocument() throws SAXException
startDocument
in interface ContentHandler
startDocument
in class DefaultHandler
SAXException
public void endDocument() throws SAXException
endDocument
in interface ContentHandler
endDocument
in class DefaultHandler
SAXException
public void startElement(String URI, String name, String qName, Attributes attributes) throws SAXException
startElement
in interface ContentHandler
startElement
in class DefaultHandler
SAXException
public 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 ContentHandler
endElement
in class DefaultHandler
SAXException
public 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 ContentHandler
characters
in class DefaultHandler
SAXException
Copyright © 2017. All rights reserved.