public class OutputUtil extends Object
Constructor and Description |
---|
OutputUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
compact(Source src,
Result target)
Serializes to a compact format, without prologue or whitespace between
elements, using UTF-8 encoding.
|
static void |
compactStream(Document dom,
OutputStream stream)
Writes a DOM document to a stream using UTF-8 encoding, in a compact
format without a prologue or whitespace between elements.
|
static void |
compactStream(Document dom,
OutputStream stream,
String encoding)
Writes a DOM document to a stream using the specified encoding, without
whitespace between elements, but with a prologue that specifes
the encoding.
|
static void |
compactStream(XMLReader reader,
OutputStream stream)
Writes XML to a stream using UTF-8 encoding, in a compact format
without prologue or whitespace between elements, using the passed
XMLReader to generate a stream of SAX events. |
static void |
compactStream(XMLReader reader,
OutputStream stream,
String encoding)
Writes XML to a stream using the specified encoding, without prologue or
whitespace between elements, using the passed
XMLReader
to generate a stream of SAX events. |
static String |
compactString(Document dom)
Writes a DOM document to a simple string format, without a prologue or
whitespace between elements.
|
static String |
compactString(XMLReader reader)
Writes XML in a simple string format, without prologue or whitespace
between elements, using the passed
XMLReader to generate
a stream of SAX events. |
static String |
elementToString(Element elem)
A simple
toString() for an element, using the format
"{NSURI}LOCALNAME "; if the element has no
namespace, the brackets remain but are empty. |
static void |
indented(Source src,
Result target,
int indent)
Serializes to a human-readable format, with each element starting on a
new line, and child elements indented a specified amount from their
parent.
|
static String |
indentedString(Document dom,
int indent)
Writes a DOM document to a string format, with indenting between
elements but without a prologue.
|
static String |
indentedString(XMLReader reader,
int indent)
Writes XML in a simple string format, without prologue or whitespace
between elements, using the passed
XMLReader to generate
a stream of SAX events. |
static String |
treeToString(Element elem,
int indent)
Debug dump of the e rooted at the specified element.
|
public static String elementToString(Element elem)
toString()
for an element, using the format
"{NSURI}LOCALNAME
"; if the element has no
namespace, the brackets remain but are empty. This is typically used
for debugging.public static String treeToString(Element elem, int indent)
elementToString(org.w3c.dom.Element)
.public static void compact(Source src, Result target)
public static void indented(Source src, Result target, int indent)
public static String compactString(Document dom)
Do not simply write this string to a file unless you use UTF-8 encoding or attach a prologue that specifies your actual encoding.
dom
- The DOM tree to be output.public static String compactString(XMLReader reader)
XMLReader
to generate
a stream of SAX events.
The transformer will call the reader's setContentHandler()
method, followed by parse()
. In the latter method, the
reader must invoke the content handler's event methods in the correct
order: at the very least, startDocument()
, followed by
startElement()
and endElement()
for the root
element, finishing with endDocument()
. Note that SAX does
not support all DOM node types: in particular, there are no comments.
Do not simply write this string to a file unless you use UTF-8 encoding or attach a prologue that specifies your actual encoding.
reader
- Provides a source of SAX events for the transformer.public static String indentedString(Document dom, int indent)
Do not simply write this string to a file unless you use UTF-8 encoding or attach a prologue that specifies the encoding.
dom
- The DOM tree to be output.indent
- The number of spaces to indent each level of the
tree. Indentation is best effort: the
javax.transform
API does not provide
any way to set indent level, so we use JDK-specific
features to achieve this, where available.
Note also that indenting will cause problems with
elements that contain mixed content, particularly
if the text elements cannot be trimmed.public static String indentedString(XMLReader reader, int indent)
XMLReader
to generate
a stream of SAX events.
The transformer will call the reader's setContentHandler()
method, followed by parse()
. In the latter method, the
reader must invoke the content handler's event methods in the correct
order: at the very least, startDocument()
, followed by
startElement()
and endElement()
for the root
element, finishing with endDocument()
. Note that SAX does
not support all DOM node types: in particular, there are no comments.
Do not simply write this string to a file unless you use UTF-8 encoding or attach a prologue that specifies the encoding.
reader
- Provides a source of SAX events for the transformer.indent
- The number of spaces to indent each level of the
tree. Indentation is best effort: the
javax.transform
API does not provide
any way to set indent level, so we use JDK-specific
features to achieve this, where available.
Note also that indenting will cause problems with
elements that contain mixed content, particularly
if the text elements cannot be trimmed.public static void compactStream(Document dom, OutputStream stream)
dom
- The DOM tree to be output.stream
- The output stream. This stream will be flushed by
this method, but will not be closed.public static void compactStream(XMLReader reader, OutputStream stream)
XMLReader
to generate a stream of SAX events.
The transformer will call the reader's setContentHandler()
method, followed by parse()
. In the latter method, the
reader must invoke the content handler's event methods in the correct
order: at the very least, startDocument()
, followed by
startElement()
and endElement()
for the root
element, finishing with endDocument()
. Note that SAX does
not support all DOM node types: in particular, there are no comments.
reader
- Provides a source of SAX events for the transformer.stream
- The output stream. This stream will be flushed by
this method, but will not be closed.public static void compactStream(Document dom, OutputStream stream, String encoding)
dom
- The DOM tree to be output.stream
- The output stream. This stream will be flushed by
this method, but will not be closed.public static void compactStream(XMLReader reader, OutputStream stream, String encoding)
XMLReader
to generate a stream of SAX events.
The transformer will call the reader's setContentHandler()
method, followed by parse()
. In the latter method, the
reader must invoke the content handler's event methods in the correct
order: at the very least, startDocument()
, followed by
startElement()
and endElement()
for the root
element, finishing with endDocument()
. Note that SAX does
not support all DOM node types: in particular, there are no comments.
reader
- Provides a source of SAX events for the transformer.stream
- The output stream. This stream will be flushed by
this method, but will not be closed.