public class XmlNodePrinter
extends Object
Prints a groovy.util.Node (as used with XmlParser) including all children in XML format.
Typical usage:
def xml = '<html><head><title>Title</title></head><body><h1>Header</h1></body></html>'
def root = new XmlParser().parseText(xml)
new XmlNodePrinter(preserveWhitespace:true).print(root.body[0])
which when run produces this on stdout (or use your own PrintWriter to direct elsewhere):
<body>
<h1>Header</h1>
</body>
| Modifiers | Name | Description |
|---|---|---|
protected static class |
XmlNodePrinter.NamespaceContext |
Tracks namespace declarations already emitted while printing a subtree. |
| Modifiers | Name | Description |
|---|---|---|
protected IndentPrinter |
out |
Printer receiving serialized XML output. |
| Constructor and description |
|---|
XmlNodePrinter(PrintWriter out)Creates a printer that writes to the supplied writer using two-space indentation and double quotes. |
XmlNodePrinter(PrintWriter out, String indent)Creates a printer that writes to the supplied writer using the supplied indentation string. |
XmlNodePrinter(PrintWriter out, String indent, String quote)Creates a printer that writes to the supplied writer using the supplied indentation and attribute quote. |
XmlNodePrinter(IndentPrinter out)Creates a printer that writes to the supplied indent printer using double quotes for attributes. |
XmlNodePrinter(IndentPrinter out, String quote)Creates a printer that writes to the supplied indent printer using the supplied attribute quote. |
XmlNodePrinter()Creates a printer that writes to standard output using default formatting. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
protected String |
getName(Object object)Resolves the printable name for a node, qualified name or plain string. |
|
public String |
getQuote()Get Quote to use when printing attributes. |
|
public boolean |
isExpandEmptyElements()Whether empty elements are expanded from <tagName/> to <tagName></tagName>. |
|
public boolean |
isNamespaceAware()Check if namespace handling is enabled. |
|
public boolean |
isPreserveWhitespace()Check if whitespace preservation is enabled. |
|
public void |
print(Node node)Prints the supplied node and its descendants. |
|
protected void |
print(Node node, XmlNodePrinter.NamespaceContext ctx)Prints a node using the supplied namespace context. |
|
protected void |
printLineBegin()Prints any indentation required before the current line. |
|
protected void |
printLineEnd()Terminates the current line without a trailing comment. |
|
protected void |
printLineEnd(String comment)Terminates the current line and optionally appends an XML comment. |
|
protected void |
printList(List list, XmlNodePrinter.NamespaceContext ctx)Prints the contents of a node value list. |
|
protected void |
printName(Node node, XmlNodePrinter.NamespaceContext ctx, boolean begin, boolean preserve)Prints an opening or closing tag for the supplied node. |
|
protected void |
printNameAttributes(Map attributes, XmlNodePrinter.NamespaceContext ctx)Prints the attributes for the current element. |
|
protected void |
printNamespace(Object object, XmlNodePrinter.NamespaceContext ctx)Prints any namespace declaration required by the supplied node or qualified name. |
|
protected void |
printSimpleItem(Object value)Prints a simple non-node value, escaping it as element content. |
|
protected boolean |
printSpecialNode(Node node)Hook for subclasses to intercept node printing. |
|
public void |
setExpandEmptyElements(boolean expandEmptyElements)Whether empty elements are expanded from <tagName/> to <tagName></tagName>. |
|
public void |
setNamespaceAware(boolean namespaceAware)Enable and/or disable namespace handling. |
|
public void |
setPreserveWhitespace(boolean preserveWhitespace)Enable and/or disable preservation of whitespace. |
|
public void |
setQuote(String quote)Set Quote to use when printing attributes. |
Printer receiving serialized XML output.
Creates a printer that writes to the supplied writer using two-space indentation and double quotes.
out - the writer receiving the serialized XMLCreates a printer that writes to the supplied writer using the supplied indentation string.
out - the writer receiving the serialized XMLindent - the indentation unit to useCreates a printer that writes to the supplied writer using the supplied indentation and attribute quote.
out - the writer receiving the serialized XMLindent - the indentation unit to usequote - the quote string to use around attribute valuesCreates a printer that writes to the supplied indent printer using double quotes for attributes.
out - the indent printer receiving the serialized XMLCreates a printer that writes to the supplied indent printer using the supplied attribute quote.
out - the indent printer receiving the serialized XMLquote - the quote string to use around attribute valuesCreates a printer that writes to standard output using default formatting.
Resolves the printable name for a node, qualified name or plain string. Subclasses may override to customize name rendering.
object - the object representing a node nameGet Quote to use when printing attributes.
Whether empty elements are expanded from <tagName/> to <tagName></tagName>.
true, if empty elements will be represented by an opening tag
followed immediately by a closing tag. Check if namespace handling is enabled.
Defaults to true.
Check if whitespace preservation is enabled.
Defaults to false.
Prints the supplied node and its descendants.
node - the root node to serializePrints a node using the supplied namespace context. Subclasses may override to customize node serialization while reusing the helper methods in this class.
node - the node to serializectx - the namespace context active for this nodePrints any indentation required before the current line.
Terminates the current line without a trailing comment.
Terminates the current line and optionally appends an XML comment.
comment - the comment text to append, or null for nonePrints the contents of a node value list.
list - the node contents to printctx - the namespace context to propagate to child nodesPrints an opening or closing tag for the supplied node.
node - the node whose name and attributes should be printedctx - the namespace context active for this nodebegin - true to print the opening tag, false for the closing tagpreserve - whether surrounding whitespace should be preservedPrints the attributes for the current element.
attributes - the attributes to printctx - the namespace context used for namespace-aware attribute namesPrints any namespace declaration required by the supplied node or qualified name.
object - the node or qualified name whose namespace should be declaredctx - the namespace context tracking declarations already emittedPrints a simple non-node value, escaping it as element content.
value - the value to printHook for subclasses to intercept node printing.
node - the node about to be printedtrue if the node was handled completely and normal printing should stopWhether empty elements are expanded from <tagName/> to <tagName></tagName>.
expandEmptyElements - if true, empty
elements will be represented by an opening tag
followed immediately by a closing tag.
Defaults to false.Enable and/or disable namespace handling.
namespaceAware - the new desired valueEnable and/or disable preservation of whitespace.
preserveWhitespace - the new desired valueSet Quote to use when printing attributes.
quote - the quote characterCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.