XML processing utilities for Apache Groovy including markup builders, parsers, and navigation tools
npx @tessl/cli install tessl/maven-org-codehaus-groovy--groovy-xml@2.5.0Groovy XML provides comprehensive XML processing capabilities for Apache Groovy, including markup builders for creating XML documents using Groovy's builder pattern, SAX and DOM parsing utilities, streaming markup support for large XML documents, and enhanced XML processing methods that integrate seamlessly with Groovy's dynamic nature.
<dependency><groupId>org.codehaus.groovy</groupId><artifactId>groovy-xml</artifactId><version>2.5.23</version></dependency>implementation 'org.codehaus.groovy:groovy-xml:2.5.23'// Core builders and parsers
import groovy.xml.MarkupBuilder;
import groovy.xml.StreamingMarkupBuilder;
import groovy.xml.DOMBuilder;
import groovy.xml.SAXBuilder;
import groovy.xml.StaxBuilder;
import groovy.xml.XmlUtil;
import groovy.util.XmlParser;
import groovy.util.XmlSlurper;
// Navigation and results
import groovy.util.slurpersupport.GPathResult;
import groovy.util.Node;
// Namespace support
import groovy.xml.Namespace;
import groovy.xml.QName;
// Utilities and entities
import groovy.xml.XmlNodePrinter;
import groovy.xml.Entity;
// Extension methods
import org.codehaus.groovy.runtime.XmlGroovyMethods;For Groovy scripts, core XML classes are often available by default through the Groovy runtime.
// Building XML with MarkupBuilder
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.root {
person(id: '1') {
name('John Doe')
email('john@example.com')
}
person(id: '2') {
name('Jane Smith')
email('jane@example.com')
}
}
println writer.toString()
// Parsing XML with XmlSlurper for navigation
def slurper = new XmlSlurper()
def root = slurper.parseText('''
<root>
<person id="1">
<name>John Doe</name>
<email>john@example.com</email>
</person>
</root>
''')
println root.person.name.text() // "John Doe"
println root.person.'@id' // "1"
// Parsing with XmlParser for manipulation
def parser = new XmlParser()
def doc = parser.parseText('<root><item>value</item></root>')
doc.item[0].value = 'new value'Groovy XML is built around several key components:
Comprehensive XML document creation using Groovy's builder pattern. Supports formatted output, streaming generation, and DOM document creation.
public class MarkupBuilder extends BuilderSupport {
public MarkupBuilder();
public MarkupBuilder(PrintWriter pw);
public MarkupBuilder(Writer writer);
public MarkupBuilder(IndentPrinter out);
public boolean getDoubleQuotes();
public void setDoubleQuotes(boolean useDoubleQuotes);
public boolean isOmitNullAttributes();
public void setOmitNullAttributes(boolean omitNullAttributes);
public MarkupBuilderHelper getMkp();
}Parse XML documents into navigable structures using either Node-based manipulation (XmlParser) or XPath-like navigation (XmlSlurper).
public class XmlParser implements ContentHandler {
public XmlParser();
public XmlParser(boolean validating, boolean namespaceAware);
public Node parse(File file) throws IOException, SAXException;
public Node parse(InputStream input) throws IOException, SAXException;
public Node parseText(String text) throws SAXException;
}
public class XmlSlurper extends DefaultHandler {
public XmlSlurper();
public XmlSlurper(boolean validating, boolean namespaceAware);
public GPathResult parse(File file) throws IOException, SAXException;
public GPathResult parse(InputStream input) throws IOException, SAXException;
public GPathResult parseText(String text) throws SAXException;
}XPath-like navigation through parsed XML using the GPathResult hierarchy, with support for filtering, traversal, and content extraction.
public abstract class GPathResult implements Writable, Buildable, Iterable<GPathResult> {
public GPathResult parent();
public GPathResult children();
public String name();
public abstract String text();
public abstract int size();
public Object getAt(int index);
public Iterator<GPathResult> iterator();
public Iterator<GPathResult> depthFirst();
public abstract GPathResult find(Closure closure);
public abstract GPathResult findAll(Closure closure);
}Utility functions for XML serialization, escaping, parser creation, and pretty printing of XML structures.
public class XmlUtil {
public static String serialize(Element element);
public static String serialize(Node node);
public static String serialize(GPathResult node);
public static String serialize(Writable writable);
public static SAXParser newSAXParser(String schemaLanguage, Source... schemas)
throws ParserConfigurationException, SAXException;
public static String escapeXml(String orig);
}Efficient streaming XML generation and processing for large documents, using StreamingMarkupBuilder and associated support classes.
class StreamingMarkupBuilder extends AbstractStreamingBuilder {
boolean useDoubleQuotes
boolean expandEmptyElements
String encoding
Writable bind(Closure closure)
Writable bindNode(Object node)
}Comprehensive namespace support including namespace-aware parsing, building, and navigation with proper prefix handling.
public class Namespace {
public Namespace();
public Namespace(String uri);
public Namespace(String uri, String prefix);
public QName get(String localName);
public String getPrefix();
public String getUri();
}JAXB (Java Architecture for XML Binding) support for marshalling and unmarshalling Java objects to/from XML.
@Deprecated
class JaxbGroovyMethods {
static <T> String marshal(Marshaller self, T jaxbElement)
static <T> T unmarshal(Unmarshaller self, String xml)
static Marshaller createMarshaller(JAXBContext self)
static Unmarshaller createUnmarshaller(JAXBContext self)
}Predefined XML and HTML entity constants for special characters and symbols.
class Entity implements Buildable {
Entity(String name)
Entity(int name)
void build(GroovyObject builder)
// Predefined entities
static final Entity lt, gt, amp, quot, apos // Core XML entities
static final Entity nbsp, copy, reg, deg // Common symbols
static final Entity eacute, ntilde, uuml // Accented characters
// ... many more predefined entities
}// Core Node type for XmlParser results
public class Node {
public String name();
public String text();
public List<Node> children();
public Map<String, String> attributes();
public Object get(String key);
public void setValue(String value);
}
// QName for namespace-aware operations
public class QName {
public QName(String localName);
public QName(String namespaceURI, String localName);
public QName(String namespaceURI, String localName, String prefix);
public String getLocalName();
public String getNamespaceURI();
public String getPrefix();
}
// Markup builder helper for special operations
public class MarkupBuilderHelper {
public void yield(Object value);
public void yieldUnescaped(Object value);
public void comment(String value);
public void xmlDeclaration(Map<String, Object> args);
}
// Extension methods support
public class XmlGroovyMethods {
public static Iterator<Node> iterator(NodeList nodeList);
public static String serialize(Element element);
}
// DOMBuilder for W3C DOM document creation
public class DOMBuilder extends BuilderSupport {
public static DOMBuilder newInstance() throws ParserConfigurationException;
public static DOMBuilder newInstance(boolean validating, boolean namespaceAware)
throws ParserConfigurationException;
public static Document parse(Reader reader)
throws IOException, SAXException, ParserConfigurationException;
public static Document parse(Reader reader, boolean validating, boolean namespaceAware)
throws IOException, SAXException, ParserConfigurationException;
public Document parseText(String text) throws SAXException, IOException;
}
// StaxBuilder for StAX-based XML processing
public class StaxBuilder extends BuilderSupport {
public StaxBuilder(XMLStreamWriter xmlStreamWriter);
}
// SAXBuilder for SAX-based XML generation
public class SAXBuilder extends BuilderSupport {
public SAXBuilder(SAXResult saxResult);
}
// Utility for converting XML to Groovy code
public class DomToGroovy {
public static void print(PrintWriter out, Element element);
public static void print(PrintWriter out, Document document);
}