CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-groovy--groovy-xml

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

Pending
Overview
Eval results
Files

xml-parsing.mddocs/

XML Parsing

Two primary parsing approaches for XML documents: tree-based parsing with XmlParser that creates mutable document trees, and GPath-based parsing with XmlSlurper that provides lazy evaluation and XPath-like navigation capabilities.

Capabilities

XmlParser

Creates a mutable tree of groovy.util.Node objects representing the parsed XML document. Ideal for scenarios requiring document modification or when working with smaller XML files where full tree loading is acceptable.

/**
 * Default constructor with standard parsing settings
 */
XmlParser()

/**
 * Constructor with validation and namespace control
 * @param validating - Enable DTD validation
 * @param namespaceAware - Enable namespace processing
 */
XmlParser(boolean validating, boolean namespaceAware)

/**
 * Constructor with full parsing control
 * @param validating - Enable DTD validation  
 * @param namespaceAware - Enable namespace processing
 * @param allowDocTypeDeclaration - Allow DOCTYPE declarations
 */
XmlParser(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration)

/**
 * Constructor using custom XMLReader
 * @param reader - Custom XMLReader instance
 */
XmlParser(XMLReader reader)

/**
 * Constructor using SAXParser
 * @param parser - SAXParser instance
 */
XmlParser(SAXParser parser)

Parsing Methods

/**
 * Parse XML from a file
 * @param file - File containing XML content
 * @return Root Node of parsed document
 */
Node parse(File file)

/**
 * Parse XML from a Path
 * @param path - Path to XML file
 * @return Root Node of parsed document  
 */
Node parse(Path path)

/**
 * Parse XML from URI/URL
 * @param uri - URI string pointing to XML resource
 * @return Root Node of parsed document
 */
Node parse(String uri)

/**
 * Parse XML from InputStream
 * @param input - InputStream containing XML
 * @return Root Node of parsed document
 */
Node parse(InputStream input)

/**
 * Parse XML from Reader
 * @param reader - Reader containing XML
 * @return Root Node of parsed document
 */
Node parse(Reader reader)

/**
 * Parse XML from InputSource
 * @param input - SAX InputSource
 * @return Root Node of parsed document
 */
Node parse(InputSource input)

/**
 * Parse XML from string content
 * @param text - String containing XML content
 * @return Root Node of parsed document
 * @throws IOException if I/O error occurs
 * @throws SAXException if parsing error occurs
 */
Node parseText(String text) throws IOException, SAXException

Configuration Methods

/**
 * Control whitespace trimming in text content
 * @param trimWhitespace - Whether to trim whitespace
 */
void setTrimWhitespace(boolean trimWhitespace)
boolean isTrimWhitespace()

/**
 * Control preservation of ignorable whitespace
 * @param keepIgnorableWhitespace - Whether to keep ignorable whitespace
 */
void setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace)
boolean isKeepIgnorableWhitespace()

/**
 * Check if parser is namespace aware
 * @return true if namespace aware
 */
boolean isNamespaceAware()

/**
 * Set namespace awareness (configuration method)
 * @param namespaceAware - true to enable namespace processing
 */
void setNamespaceAware(boolean namespaceAware)

XMLReader Delegation Methods

/**
 * Get DTD handler for parser
 * @return Current DTDHandler
 */
DTDHandler getDTDHandler()

/**
 * Set DTD handler for parser
 * @param dtdHandler - DTDHandler instance
 */
void setDTDHandler(DTDHandler dtdHandler)

/**
 * Get entity resolver for parser
 * @return Current EntityResolver
 */
EntityResolver getEntityResolver()

/**
 * Set entity resolver for parser
 * @param entityResolver - EntityResolver instance
 */
void setEntityResolver(EntityResolver entityResolver)

/**
 * Get error handler for parser
 * @return Current ErrorHandler
 */
ErrorHandler getErrorHandler()

/**
 * Set error handler for parser
 * @param errorHandler - ErrorHandler instance
 */
void setErrorHandler(ErrorHandler errorHandler)

/**
 * Get parser feature value
 * @param uri - Feature URI
 * @return Feature value
 * @throws SAXNotRecognizedException if feature not recognized
 * @throws SAXNotSupportedException if feature not supported
 */
boolean getFeature(String uri) throws SAXNotRecognizedException, SAXNotSupportedException

/**
 * Set parser feature value
 * @param uri - Feature URI
 * @param value - Feature value
 * @throws SAXNotRecognizedException if feature not recognized
 * @throws SAXNotSupportedException if feature not supported
 */
void setFeature(String uri, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException

/**
 * Get parser property value
 * @param uri - Property URI
 * @return Property value
 * @throws SAXNotRecognizedException if property not recognized
 * @throws SAXNotSupportedException if property not supported
 */
Object getProperty(String uri) throws SAXNotRecognizedException, SAXNotSupportedException

/**
 * Set parser property value
 * @param uri - Property URI
 * @param value - Property value
 * @throws SAXNotRecognizedException if property not recognized
 * @throws SAXNotSupportedException if property not supported
 */
void setProperty(String uri, Object value) throws SAXNotRecognizedException, SAXNotSupportedException

Usage Examples:

import groovy.xml.XmlParser

// Basic parsing
def parser = new XmlParser()
def root = parser.parseText('<books><book title="Groovy"/></books>')
println root.book[0].@title  // "Groovy"

// Namespace-aware parsing
def nsParser = new XmlParser(false, true)
def nsRoot = nsParser.parseText('''
    <books xmlns:lib="http://library.org">
        <lib:book title="Advanced Groovy"/>
    </books>
''')

// File parsing with error handling
try {
    def fileRoot = parser.parse(new File("data.xml"))
    fileRoot.book.each { book ->
        println "Title: ${book.@title}, Author: ${book.@author}"
    }
} catch (Exception e) {
    println "Parsing failed: ${e.message}"
}

// Whitespace control
parser.setTrimWhitespace(true)
parser.setKeepIgnorableWhitespace(false)

XmlSlurper

Provides lazy-evaluated GPath expressions for XML navigation. Ideal for read-only access to XML documents, especially when working with large files or when only specific parts of the document are needed.

/**
 * Default constructor with standard parsing settings
 */
XmlSlurper()

/**
 * Constructor with validation and namespace control
 * @param validating - Enable DTD validation
 * @param namespaceAware - Enable namespace processing  
 */
XmlSlurper(boolean validating, boolean namespaceAware)

/**
 * Constructor with full parsing control
 * @param validating - Enable DTD validation
 * @param namespaceAware - Enable namespace processing
 * @param allowDocTypeDeclaration - Allow DOCTYPE declarations
 */
XmlSlurper(boolean validating, boolean namespaceAware, boolean allowDocTypeDeclaration)

/**
 * Constructor using custom XMLReader
 * @param reader - Custom XMLReader instance
 */
XmlSlurper(XMLReader reader)

/**
 * Constructor using SAXParser
 * @param parser - SAXParser instance
 */
XmlSlurper(SAXParser parser)

Parsing Methods

/**
 * Parse XML from InputSource
 * @param input - SAX InputSource
 * @return GPathResult for navigation
 */
GPathResult parse(InputSource input)

/**
 * Parse XML from File
 * @param file - File containing XML
 * @return GPathResult for navigation
 */
GPathResult parse(File file)

/**
 * Parse XML from InputStream
 * @param input - InputStream containing XML
 * @return GPathResult for navigation
 */
GPathResult parse(InputStream input)

/**
 * Parse XML from Reader
 * @param reader - Reader containing XML
 * @return GPathResult for navigation
 */
GPathResult parse(Reader reader)

/**
 * Parse XML from URI/URL
 * @param uri - URI string pointing to XML resource
 * @return GPathResult for navigation
 */
GPathResult parse(String uri)

/**
 * Parse XML from Path
 * @param path - Path to XML file
 * @return GPathResult for navigation
 */
GPathResult parse(Path path)

/**
 * Parse XML from string content
 * @param text - String containing XML content
 * @return GPathResult for navigation
 */
GPathResult parseText(String text)

Configuration Methods

/**
 * Control preservation of ignorable whitespace
 * @param keepIgnorableWhitespace - Whether to preserve ignorable whitespace
 */
void setKeepIgnorableWhitespace(boolean keepIgnorableWhitespace)
boolean isKeepIgnorableWhitespace()

/**
 * Get the parsed document as GPathResult
 * @return The root GPathResult
 */
GPathResult getDocument()

/**
 * Set base URL for entity resolution
 * @param base - Base URL for relative entity references
 */
void setEntityBaseUrl(URL base)

XMLReader Delegation Methods (XmlSlurper)

/**
 * Get DTD handler for slurper
 * @return Current DTDHandler
 */
DTDHandler getDTDHandler()

/**
 * Set DTD handler for slurper
 * @param dtdHandler - DTDHandler instance
 */
void setDTDHandler(DTDHandler dtdHandler)

/**
 * Get entity resolver for slurper
 * @return Current EntityResolver
 */
EntityResolver getEntityResolver()

/**
 * Set entity resolver for slurper
 * @param entityResolver - EntityResolver instance
 */
void setEntityResolver(EntityResolver entityResolver)

/**
 * Get error handler for slurper
 * @return Current ErrorHandler
 */
ErrorHandler getErrorHandler()

/**
 * Set error handler for slurper
 * @param errorHandler - ErrorHandler instance
 */
void setErrorHandler(ErrorHandler errorHandler)

/**
 * Get slurper feature value
 * @param uri - Feature URI
 * @return Feature value
 * @throws SAXNotRecognizedException if feature not recognized
 * @throws SAXNotSupportedException if feature not supported
 */
boolean getFeature(String uri) throws SAXNotRecognizedException, SAXNotSupportedException

/**
 * Set slurper feature value
 * @param uri - Feature URI
 * @param value - Feature value
 * @throws SAXNotRecognizedException if feature not recognized
 * @throws SAXNotSupportedException if feature not supported
 */
void setFeature(String uri, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException

/**
 * Get slurper property value
 * @param uri - Property URI
 * @return Property value
 * @throws SAXNotRecognizedException if property not recognized
 * @throws SAXNotSupportedException if property not supported
 */
Object getProperty(String uri) throws SAXNotRecognizedException, SAXNotSupportedException

/**
 * Set slurper property value
 * @param uri - Property URI
 * @param value - Property value
 * @throws SAXNotRecognizedException if property not recognized
 * @throws SAXNotSupportedException if property not supported
 */
void setProperty(String uri, Object value) throws SAXNotRecognizedException, SAXNotSupportedException

Usage Examples:

import groovy.xml.XmlSlurper

// Basic GPath navigation
def slurper = new XmlSlurper()
def books = slurper.parseText('''
    <library>
        <book id="1" author="Venkat">
            <title>Programming Groovy</title>
            <price>45.99</price>
        </book>
        <book id="2" author="Dierk">
            <title>Groovy in Action</title>
            <price>55.99</price>
        </book>
    </library>
''')

// GPath navigation examples
println books.book.size()           // 2
println books.book[0].@id          // "1"
println books.book[0].@author      // "Venkat"
println books.book[0].title.text() // "Programming Groovy"
println books.book*.@author        // ["Venkat", "Dierk"]
println books.book.title*.text()   // ["Programming Groovy", "Groovy in Action"]

// Find operations
def expensiveBooks = books.book.findAll { 
    it.price.text().toDouble() > 50.0 
}
println expensiveBooks.size()       // 1

// Namespace handling
def nsSlurper = new XmlSlurper(false, true)
def catalog = nsSlurper.parseText('''
    <catalog xmlns:book="http://books.org">
        <book:item isbn="123">
            <book:title>XML Processing</book:title>
        </book:item>
    </catalog>
''')

GPathResult Operations

The result type returned by XmlSlurper provides extensive navigation and querying capabilities.

/**
 * Get element name
 * @return Element name as string
 */
String name()

/**
 * Get text content of element and children
 * @return Combined text content
 */
String text()

/**
 * Get number of child elements
 * @return Count of children
 */
int size()

/**
 * Check if result is empty
 * @return true if no elements
 */
boolean isEmpty()

/**
 * Get parent element
 * @return Parent GPathResult
 */
GPathResult parent()

/**
 * Get all children
 * @return Children as GPathResult
 */
GPathResult children()

/**
 * Navigate to parents
 * @return All parent elements
 */
GPathResult parents()

/**
 * Iterator over child nodes
 * @return Iterator of child nodes
 */
Iterator iterator()

/**
 * Iterator over all child nodes including text
 * @return Iterator of all child content
 */
Iterator childNodes()

/**
 * Find first element matching closure
 * @param closure - Matching predicate
 * @return First matching GPathResult
 */
GPathResult find(Closure closure)

/**
 * Find all elements matching closure
 * @param closure - Matching predicate  
 * @return All matching elements as GPathResult
 */
GPathResult findAll(Closure closure)

/**
 * Depth-first traversal iterator
 * @return Iterator for depth-first traversal
 */
Iterator depthFirst()

/**
 * Breadth-first traversal iterator
 * @return Iterator for breadth-first traversal
 */
Iterator breadthFirst()

Usage Examples:

// Advanced GPath operations
def data = slurper.parseText('''
    <company>
        <department name="Engineering">
            <employee id="1" name="Alice"/>
            <employee id="2" name="Bob"/>
        </department>
        <department name="Sales">
            <employee id="3" name="Charlie"/>
        </department>
    </company>
''')

// Navigation and querying
println data.department.size()                    // 2
println data.department.@name                     // ["Engineering", "Sales"]
println data.department.employee.@name            // ["Alice", "Bob", "Charlie"]

// Finding specific elements
def engineering = data.department.find { it.@name == "Engineering" }
println engineering.employee.size()               // 2

def alice = data.department.employee.find { it.@name == "Alice" }
println alice.@id                                 // "1"

// Tree traversal
data.depthFirst().each { node ->
    if (node.name() == "employee") {
        println "Employee: ${node.@name}"
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-groovy--groovy-xml

docs

dom-processing.md

index.md

namespace-support.md

xml-generation.md

xml-parsing.md

xml-utilities.md

tile.json