Package groovy.xml

# Class XmlNodePrinter

java.lang.Object
groovy.xml.XmlNodePrinter

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>

See Also:
  - `NodePrinter`
  - `XmlUtil.serialize(Node)`

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `protected static class`
  `XmlNodePrinter.NamespaceContext`
  Tracks namespace declarations already emitted while printing a subtree.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected final IndentPrinter`
  `out`
  Printer receiving serialized XML output.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `XmlNodePrinter()`
  Creates a printer that writes to standard output using default formatting.
  `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(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.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected String`
  `getName(Object object)`
  Resolves the printable name for a node, qualified name or plain string.
  `String`
  `getQuote()`
  Get Quote to use when printing attributes.
  `boolean`
  `isExpandEmptyElements()`
  Whether empty elements are expanded from <tagName/> to <tagName></tagName>.
  `boolean`
  `isNamespaceAware()`
  Check if namespace handling is enabled.
  `boolean`
  `isPreserveWhitespace()`
  Check if whitespace preservation is enabled.
  `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.
  `void`
  `setExpandEmptyElements(boolean expandEmptyElements)`
  Whether empty elements are expanded from <tagName/> to <tagName></tagName>.
  `void`
  `setNamespaceAware(boolean namespaceAware)`
  Enable and/or disable namespace handling.
  `void`
  `setPreserveWhitespace(boolean preserveWhitespace)`
  Enable and/or disable preservation of whitespace.
  `void`
  `setQuote(String quote)`
  Set Quote to use when printing attributes.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### out

    protected final IndentPrinter out
    Printer receiving serialized XML output.

- ## Constructor Details

  - ### XmlNodePrinter

    public XmlNodePrinter(PrintWriter out)
    Creates a printer that writes to the supplied writer using two-space indentation and double quotes.
    Parameters:
      `out` - the writer receiving the serialized XML

  - ### XmlNodePrinter

    public XmlNodePrinter(PrintWriter out, String indent)
    Creates a printer that writes to the supplied writer using the supplied indentation string.
    Parameters:
      `out` - the writer receiving the serialized XML
      `indent` - the indentation unit to use

  - ### XmlNodePrinter

    public XmlNodePrinter(PrintWriter out, String indent, String quote)
    Creates a printer that writes to the supplied writer using the supplied indentation and attribute quote.
    Parameters:
      `out` - the writer receiving the serialized XML
      `indent` - the indentation unit to use
      `quote` - the quote string to use around attribute values

  - ### XmlNodePrinter

    public XmlNodePrinter(IndentPrinter out)
    Creates a printer that writes to the supplied indent printer using double quotes for attributes.
    Parameters:
      `out` - the indent printer receiving the serialized XML

  - ### XmlNodePrinter

    public XmlNodePrinter(IndentPrinter out, String quote)
    Creates a printer that writes to the supplied indent printer using the supplied attribute quote.
    Parameters:
      `out` - the indent printer receiving the serialized XML
      `quote` - the quote string to use around attribute values

  - ### XmlNodePrinter

    public XmlNodePrinter()
    Creates a printer that writes to standard output using default formatting.

- ## Method Details

  - ### print

    public void print(Node node)
    Prints the supplied node and its descendants.
    Parameters:
      `node` - the root node to serialize

  - ### isNamespaceAware

    public boolean isNamespaceAware()
    Check if namespace handling is enabled. Defaults to `true`.
    Returns:
      true if namespace handling is enabled

  - ### setNamespaceAware

    public void setNamespaceAware(boolean namespaceAware)
    Enable and/or disable namespace handling.
    Parameters:
      `namespaceAware` - the new desired value

  - ### isPreserveWhitespace

    public boolean isPreserveWhitespace()
    Check if whitespace preservation is enabled. Defaults to `false`.
    Returns:
      true if whitespaces are honoured when printing simple text nodes

  - ### setPreserveWhitespace

    public void setPreserveWhitespace(boolean preserveWhitespace)
    Enable and/or disable preservation of whitespace.
    Parameters:
      `preserveWhitespace` - the new desired value

  - ### getQuote

    public String getQuote()
    Get Quote to use when printing attributes.
    Returns:
      the quote character

  - ### setQuote

    public void setQuote(String quote)
    Set Quote to use when printing attributes.
    Parameters:
      `quote` - the quote character

  - ### isExpandEmptyElements

    public boolean isExpandEmptyElements()
    Whether empty elements are expanded from <tagName/> to <tagName></tagName>.
    Returns:
      `true`, if empty elements will be represented by an opening tag followed immediately by a closing tag.

  - ### setExpandEmptyElements

    public void setExpandEmptyElements(boolean expandEmptyElements)
    Whether empty elements are expanded from <tagName/> to <tagName></tagName>.
    Parameters:
      `expandEmptyElements` - if `true`, empty elements will be represented by an opening tag followed immediately by a closing tag. Defaults to `false`.

  - ### print

    protected void print(Node node, XmlNodePrinter.NamespaceContext ctx)
    Prints a node using the supplied namespace context. Subclasses may override to customize node serialization while reusing the helper methods in this class.
    Parameters:
      `node` - the node to serialize
      `ctx` - the namespace context active for this node

  - ### printLineBegin

    protected void printLineBegin()
    Prints any indentation required before the current line.

  - ### printLineEnd

    protected void printLineEnd()
    Terminates the current line without a trailing comment.

  - ### printLineEnd

    protected void printLineEnd(String comment)
    Terminates the current line and optionally appends an XML comment.
    Parameters:
      `comment` - the comment text to append, or `null` for none

  - ### printList

    protected void printList(List list, XmlNodePrinter.NamespaceContext ctx)
    Prints the contents of a node value list.
    Parameters:
      `list` - the node contents to print
      `ctx` - the namespace context to propagate to child nodes

  - ### printSimpleItem

    protected void printSimpleItem(Object value)
    Prints a simple non-node value, escaping it as element content.
    Parameters:
      `value` - the value to print

  - ### printName

    protected void printName(Node node, XmlNodePrinter.NamespaceContext ctx, boolean begin, boolean preserve)
    Prints an opening or closing tag for the supplied node.
    Parameters:
      `node` - the node whose name and attributes should be printed
      `ctx` - the namespace context active for this node
      `begin` - `true` to print the opening tag, `false` for the closing tag
      `preserve` - whether surrounding whitespace should be preserved

  - ### printSpecialNode

    protected boolean printSpecialNode(Node node)
    Hook for subclasses to intercept node printing.
    Parameters:
      `node` - the node about to be printed
    Returns:
      `true` if the node was handled completely and normal printing should stop

  - ### printNamespace

    protected void printNamespace(Object object, XmlNodePrinter.NamespaceContext ctx)
    Prints any namespace declaration required by the supplied node or qualified name.
    Parameters:
      `object` - the node or qualified name whose namespace should be declared
      `ctx` - the namespace context tracking declarations already emitted

  - ### printNameAttributes

    protected void printNameAttributes(Map attributes, XmlNodePrinter.NamespaceContext ctx)
    Prints the attributes for the current element.
    Parameters:
      `attributes` - the attributes to print
      `ctx` - the namespace context used for namespace-aware attribute names

  - ### getName

    protected String getName(Object object)
    Resolves the printable name for a node, qualified name or plain string. Subclasses may override to customize name rendering.
    Parameters:
      `object` - the object representing a node name
    Returns:
      the printable name