Groovy XML support library providing comprehensive XML processing capabilities including parsing, manipulation, and generation of XML documents using Groovy's native XML handling features, XmlSlurper, and XmlParser APIs
npx @tessl/cli install tessl/maven-org-apache-groovy--groovy-xml@5.0.0A comprehensive Groovy library for XML processing providing powerful and idiomatic APIs for parsing, generating, and manipulating XML documents. The library offers multiple processing approaches from simple parsing to advanced streaming operations, all designed to leverage Groovy's dynamic features for intuitive XML handling.
implementation 'org.apache.groovy:groovy-xml:5.0.0'import groovy.xml.*Specific imports for common use cases:
import groovy.xml.XmlParser
import groovy.xml.XmlSlurper
import groovy.xml.MarkupBuilder
import groovy.xml.DOMBuilder
import groovy.xml.XmlUtilimport groovy.xml.*
// Parse XML with XmlParser
def parser = new XmlParser()
def root = parser.parseText('''
<books>
<book id="1" title="Groovy in Action"/>
<book id="2" title="Programming Groovy"/>
</books>
''')
// Access parsed data
println root.book[0].@title // "Groovy in Action"
println root.book.size() // 2
// Generate XML with MarkupBuilder
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.books {
book(id: "1", title: "Groovy in Action")
book(id: "2", title: "Programming Groovy")
}
println writer.toString()
// Parse with XmlSlurper for GPath navigation
def slurper = new XmlSlurper()
def doc = slurper.parseText('''<catalog><book price="12.99">Title</book></catalog>''')
println doc.book.@price // "12.99"
println doc.book.text() // "Title"The Groovy XML library is built around several key processing paradigms:
XmlParser creates mutable DOM-like trees using groovy.util.NodeXmlSlurper provides lazy-evaluated GPath expressions for XML traversalMarkupBuilder, DOMBuilder, StreamingMarkupBuilder) for XML generationTwo primary parsing approaches: tree-based parsing with XmlParser for mutable document trees, and GPath-based parsing with XmlSlurper for lazy evaluation and XPath-like navigation.
class XmlParser {
XmlParser()
XmlParser(boolean validating, boolean namespaceAware)
Node parse(File file)
Node parse(String uri)
Node parseText(String text)
}
class XmlSlurper {
XmlSlurper()
XmlSlurper(boolean validating, boolean namespaceAware)
GPathResult parse(File file)
GPathResult parse(String uri)
GPathResult parseText(String text)
}Comprehensive XML building capabilities including markup builders for formatted output, DOM builders for W3C DOM integration, and streaming builders for memory-efficient large document generation.
class MarkupBuilder {
MarkupBuilder()
MarkupBuilder(Writer writer)
MarkupBuilderHelper getMkp()
}
class DOMBuilder {
static DOMBuilder newInstance()
Document parseText(String text)
}
class StreamingMarkupBuilder {
def bind(Closure closure)
def bindNode(Node node)
}
class Entity {
Entity(String name)
Entity(int code)
// Comprehensive XML entity constants (nbsp, copy, lt, gt, etc.)
}Enhanced DOM processing with Groovy category methods providing GPath-style navigation and manipulation for standard W3C DOM objects.
class DOMCategory {
static Object get(Element element, String elementName)
static String text(Node node)
static Element appendNode(Element self, Object name)
static NodeList depthFirst(Element self)
static String xpath(Node self, String expression)
}Utility functions for XML serialization, pretty printing, escaping, and advanced parser configuration with schema validation support.
class XmlUtil {
static String serialize(Element element)
static String serialize(Node node)
static String serialize(GPathResult node)
static String escapeXml(String text)
static SAXParser newSAXParser(String schemaLanguage, Source... schemas)
}Comprehensive XML namespace support including namespace builders, QName factories, and namespace-aware parsing and generation across all processing modes.
class Namespace {
Namespace(String uri)
Namespace(String uri, String prefix)
QName get(String localName)
}
class NamespaceBuilder {
NamespaceBuilderSupport namespace(String uri)
NamespaceBuilderSupport declareNamespace(Map ns)
}// Core result types
class Node {
String name()
Object value()
List children()
Map attributes()
Node parent()
}
abstract class GPathResult {
String name()
String text()
int size()
boolean isEmpty()
GPathResult parent()
Iterator iterator()
}
// Builder helper types
class MarkupBuilderHelper {
void yield(Object value)
void comment(String value)
void xmlDeclaration(Map<String, Object> args)
}
// Exception types
class SAXException extends Exception
class ParserConfigurationException extends Exception