class StaxBuilder
extends BuilderSupport
A Groovy builder that emits XML using a StAX writer.
Typical usage:
import javax.xml.stream.XMLOutputFactory
def factory = XMLOutputFactory.newInstance()
def writer = new StringWriter()
def builder = new groovy.xml.StaxBuilder(factory.createXMLStreamWriter(writer))
builder.root1(a:5, b:7) {
elem1('hello1')
elem2('hello2')
elem3(x:7)
}
def pretty = writer.toString()
.replaceAll(/<\?xml[^>]*>/, '') // remove XML declaration
assert pretty in ['hello1 hello2 ',
'hello1 hello2 ']
StAX-compatible writers from external libraries, such as Jettison, may also be used.
| Constructor and description |
|---|
StaxBuilder(Object xmlStreamWriter)Creates a builder that writes a new XML document to the supplied StAX writer. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
protected Object |
createNode(Object name)Creates an element with no attributes or body text. |
|
protected Object |
createNode(Object name, Object value)Creates an element containing simple text content. |
|
protected Object |
createNode(Object name, Map attributes)Creates an element with attributes and no body text. |
|
protected Object |
createNode(Object name, Map attributes, Object value)Creates an element and writes any supplied attributes and text content. |
|
protected void |
nodeCompleted(Object parent, Object node)Closes the current element and finalizes the document when the root node completes. |
|
protected void |
setParent(Object parent, Object child)No-op for StAX output because parent relationships are managed by the stream writer itself. |
| Methods inherited from class | Name |
|---|---|
class BuilderSupport |
createNode, createNode, createNode, createNode, doInvokeMethod, getCurrent, getName, invokeMethod, invokeMethod, nodeCompleted, postNodeCompletion, setClosureDelegate, setCurrent, setParent |
class GroovyObjectSupport |
getMetaClass, setMetaClass |
Writer receiving the XML document generated by this builder.
Creates a builder that writes a new XML document to the supplied StAX writer.
xmlStreamWriter - writer that receives generated XML eventsCreates an element with no attributes or body text.
name - element nameCreates an element containing simple text content.
name - element namevalue - text value to write inside the elementCreates an element with attributes and no body text.
name - element nameattributes - attribute values to write on the elementCreates an element and writes any supplied attributes and text content.
name - element nameattributes - attribute values to write on the elementvalue - text value to write inside the elementCloses the current element and finalizes the document when the root node completes.
parent - parent node token, or null for the document rootnode - completed node tokenNo-op for StAX output because parent relationships are managed by the stream writer itself.
parent - parent node tokenchild - child node tokenCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.