CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-codehaus-groovy--groovy-xml

XML processing utilities for Apache Groovy including markup builders, parsers, and navigation tools

Pending
Overview
Eval results
Files

utilities.mddocs/

XML Utilities

Groovy XML provides comprehensive utility classes for XML serialization, pretty printing, escaping, and parser configuration.

XmlUtil

Central utility class providing static methods for XML serialization, parser creation, and content escaping.

public class XmlUtil {
    // Serialization methods for various types
    public static String serialize(Element element);
    public static void serialize(Element element, OutputStream os) throws IOException;
    public static void serialize(Element element, Writer w) throws IOException;
    public static String serialize(Node node);
    public static void serialize(Node node, OutputStream os) throws IOException;
    public static void serialize(Node node, Writer w) throws IOException;
    public static String serialize(GPathResult node);
    public static void serialize(GPathResult node, OutputStream os) throws IOException;
    public static void serialize(GPathResult node, Writer w) throws IOException;
    public static String serialize(Writable writable);
    public static void serialize(Writable writable, OutputStream os) throws IOException;
    public static void serialize(Writable writable, Writer w) throws IOException;
    public static String serialize(String xmlString);
    public static void serialize(String xmlString, OutputStream os) throws IOException;
    public static void serialize(String xmlString, Writer w) throws IOException;
    
    // SAX Parser factory methods
    public static SAXParser newSAXParser(String schemaLanguage, Source... schemas)
        throws ParserConfigurationException, SAXException;
    public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, 
        boolean validating, Source... schemas) throws ParserConfigurationException, SAXException;
    public static SAXParser newSAXParser(String schemaLanguage, File schema)
        throws ParserConfigurationException, SAXException;
    public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, 
        boolean validating, File schema) throws ParserConfigurationException, SAXException;
    public static SAXParser newSAXParser(String schemaLanguage, URL schema)
        throws ParserConfigurationException, SAXException;
    public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, 
        boolean validating, URL schema) throws ParserConfigurationException, SAXException;
    
    // XML escaping utilities
    public static String escapeXml(String orig);
    public static String escapeControlCharacters(String orig);
}

XmlUtil Usage Examples

// Serialize different types of XML objects
def parser = new XmlParser()
def node = parser.parseText('<root><item>value</item></root>')

// Serialize Node to string
def xmlString = XmlUtil.serialize(node)
println xmlString  // Formatted XML string

// Serialize to OutputStream
def baos = new ByteArrayOutputStream()
XmlUtil.serialize(node, baos)
println baos.toString()

// Serialize to Writer
def writer = new StringWriter()
XmlUtil.serialize(node, writer)
println writer.toString()

// Serialize GPathResult
def slurper = new XmlSlurper()
def gpath = slurper.parseText('<root><item>value</item></root>')
def serialized = XmlUtil.serialize(gpath)

// Serialize DOM Element
def domBuilder = DOMBuilder.newInstance()
def doc = domBuilder.root { item('value') }
def element = doc.documentElement
def domSerialized = XmlUtil.serialize(element)

// Serialize Writable (from StreamingMarkupBuilder)
def smb = new StreamingMarkupBuilder()
def writable = smb.bind { root { item('value') } }
def writableSerialized = XmlUtil.serialize(writable)

// Pretty-print existing XML string
def uglyXml = '<root><item>value</item><item2>value2</item2></root>'
def prettyXml = XmlUtil.serialize(uglyXml)
println prettyXml  // Nicely formatted

XML Escaping

// Escape special XML characters
def rawText = 'This contains <special> & "characters" that need \'escaping\''
def escaped = XmlUtil.escapeXml(rawText)
println escaped  // "This contains &lt;special&gt; &amp; &quot;characters&quot; that need &apos;escaping&apos;"

// Escape control characters
def controlText = "Line 1\u0001\nLine 2\u0002"
def escapedControl = XmlUtil.escapeControlCharacters(controlText)
println escapedControl

// Use in builders
def xml = new MarkupBuilder(writer)
xml.root {
    content(XmlUtil.escapeXml(userInput))
    // or let the builder handle escaping automatically
    autoEscaped(userInput)
}

SAX Parser Factory

// Create SAX parser with schema validation
def xsdFile = new File('schema.xsd')
def parser = XmlUtil.newSAXParser(XMLConstants.W3C_XML_SCHEMA_NS_URI, xsdFile)

// Create with namespace awareness and validation
def validatingParser = XmlUtil.newSAXParser(
    XMLConstants.W3C_XML_SCHEMA_NS_URI,
    true,  // namespace aware
    true,  // validating
    xsdFile
)

// Create with multiple schemas
def schema1 = new StreamSource(new File('schema1.xsd'))
def schema2 = new StreamSource(new File('schema2.xsd'))
def multiSchemaParser = XmlUtil.newSAXParser(
    XMLConstants.W3C_XML_SCHEMA_NS_URI,
    schema1, schema2
)

// Use with XmlParser
def xmlParser = new XmlParser(validatingParser)
def validatedNode = xmlParser.parse(new File('document.xml'))

XmlNodePrinter

Specialized pretty-printer for Node structures with extensive formatting options.

public class XmlNodePrinter {
    // Constructors
    public XmlNodePrinter();
    public XmlNodePrinter(PrintWriter out);
    public XmlNodePrinter(PrintWriter out, String indent);
    public XmlNodePrinter(PrintWriter out, String indent, String quote);
    public XmlNodePrinter(IndentPrinter out);
    public XmlNodePrinter(IndentPrinter out, String quote);
    
    // Main printing method
    public void print(Node node);
    
    // Configuration methods
    public boolean isNamespaceAware();
    public void setNamespaceAware(boolean namespaceAware);
    public boolean isPreserveWhitespace();
    public void setPreserveWhitespace(boolean preserveWhitespace);
    public String getQuote();
    public void setQuote(String quote);
    public boolean isExpandEmptyElements();
    public void setExpandEmptyElements(boolean expandEmptyElements);
}

XmlNodePrinter Usage

def parser = new XmlParser()
def node = parser.parseText('''
    <root xmlns:ns="http://example.com">
        <item id="1">Value 1</item>
        <item id="2"></item>
        <ns:special>Namespaced content</ns:special>
    </root>
''')

// Basic pretty printing
def writer = new StringWriter()
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.print(node)
println writer.toString()

// Customized formatting
def customWriter = new StringWriter()
def customPrinter = new XmlNodePrinter(
    new PrintWriter(customWriter),
    "    ",  // 4-space indent
    "\""     // double quotes for attributes
)

// Configure additional options
customPrinter.setNamespaceAware(true)
customPrinter.setPreserveWhitespace(false)
customPrinter.setExpandEmptyElements(true)  // <item></item> instead of <item/>

customPrinter.print(node)
println customWriter.toString()

// Using IndentPrinter for more control
def indentWriter = new StringWriter()
def indentPrinter = new IndentPrinter(indentWriter, "  ")
def nodePrinter = new XmlNodePrinter(indentPrinter, "'")  // single quotes

nodePrinter.print(node)
println indentWriter.toString()

Extension Methods (XmlGroovyMethods)

Extension methods that add XML functionality to existing Java classes.

public class XmlGroovyMethods {
    // Extends NodeList to make it iterable
    public static Iterator<Node> iterator(NodeList nodeList);
    
    // Extends Element for convenient serialization
    public static String serialize(Element element);
}

Extension Methods Usage

// Working with DOM NodeList
def domDoc = DOMBuilder.parse(new StringReader('<root><item/><item/></root>'))
def nodeList = domDoc.getElementsByTagName('item')

// NodeList is now iterable thanks to extension method
nodeList.each { element ->
    println "Element: ${element.tagName}"
}

// Direct iteration
for (element in nodeList) {
    println "Element: ${element.tagName}"
}

// DOM Element serialization
def rootElement = domDoc.documentElement
def serialized = rootElement.serialize()  // Extension method
println serialized

Factory Classes

FactorySupport

Base class for XML factory implementations.

public class FactorySupport {
    // Configuration methods for SAX factories
    protected void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory);
    protected void setSAXParserFactory(SAXParserFactory saxParserFactory);
    protected void setTransformerFactory(TransformerFactory transformerFactory);
}

XmlParserFactory

Factory for creating XmlParser instances (primarily for migration support).

class XmlParserFactory {
    static Object newParser(Object... args)
}

XmlSlurperFactory

Factory for creating XmlSlurper instances.

class XmlSlurperFactory {
    static Object newSlurper(Object... args)
}

Factory Usage

// Using parser factory
def parser = XmlParserFactory.newParser(
    true,  // validating
    true   // namespace aware
)

// Using slurper factory
def slurper = XmlSlurperFactory.newSlurper(false, true)

// Custom factory configurations
def customFactory = new FactorySupport() {
    // Override factory configuration
}

Validation and Schema Support

// Create validating parsers with schemas
def createValidatingParser = { schemaFile ->
    def parser = XmlUtil.newSAXParser(
        XMLConstants.W3C_XML_SCHEMA_NS_URI,
        true,  // namespace aware  
        true,  // validating
        schemaFile
    )
    return new XmlParser(parser)
}

// Validate against XSD
def xsdFile = new File('catalog.xsd')
def validatingParser = createValidatingParser(xsdFile)

try {
    def validatedDoc = validatingParser.parse(new File('catalog.xml'))
    println "Document is valid"
} catch (SAXException e) {
    println "Validation error: ${e.message}"
}

// RelaxNG validation
def relaxNGSchema = new File('schema.rng')
def relaxNGParser = XmlUtil.newSAXParser(
    XMLConstants.RELAXNG_NS_URI,
    relaxNGSchema
)

Performance Utilities

// Efficient serialization for large documents
def serializeLargeDocument = { node, outputFile ->
    new FileOutputStream(outputFile).withStream { fos ->
        XmlUtil.serialize(node, fos)
    }
}

// Memory-efficient pretty printing
def prettyPrintToFile = { node, outputFile ->
    new FileWriter(outputFile).withWriter { writer ->
        def printer = new XmlNodePrinter(new PrintWriter(writer))
        printer.print(node)
    }
}

// Streaming serialization
def streamingSerialize = { writable, outputStream ->
    XmlUtil.serialize(writable, outputStream)
}

Utility Best Practices

// Always escape user input
def safeXmlContent = { userInput ->
    return XmlUtil.escapeXml(userInput ?: "")
}

// Use appropriate serialization method
def serializeBasedOnType = { xmlObject ->
    switch (xmlObject) {
        case Node:
            return XmlUtil.serialize(xmlObject as Node)
        case GPathResult:
            return XmlUtil.serialize(xmlObject as GPathResult)
        case Element:
            return XmlUtil.serialize(xmlObject as Element)
        case Writable:
            return XmlUtil.serialize(xmlObject as Writable)
        default:
            return XmlUtil.serialize(xmlObject.toString())
    }
}

// Configure printers for consistent output
def createStandardPrinter = { writer ->
    def printer = new XmlNodePrinter(new PrintWriter(writer), "  ", "\"")
    printer.setNamespaceAware(true)
    printer.setPreserveWhitespace(false)
    printer.setExpandEmptyElements(false)
    return printer
}

Install with Tessl CLI

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

docs

builders.md

entities.md

index.md

jaxb.md

namespaces.md

navigation.md

parsing.md

streaming.md

utilities.md

tile.json