Skip to: Site menu | Main content

Groovy 

      Download | Documentation | Developers | Community

An agile dynamic language for the Java Platform

Processing XML Add comment to Wiki View in Wiki Edit Wiki page Printable Version

Processing existing XML

Groovy provides special XML processing support through the following classes:

Technology When/Why to use Requirements
XmlParser supports GPath expressions for XML documents and allows updating -
XmlSlurper lower overheads than XmlParser due to lazy evaluation but only supports read operations -
DOMCategory low-level tree-based processing where you want some syntactic sugar place use(DOMCategory) around your code

If you have special needs, you can use one of the many available Java APIs for XML processing. You should consult the documentation of individual APIs for the details, but some examples to get you started are included here:

Technology When/Why to use Requirements
DOM low-level tree-based processing -
SAX event-based push-style parsing can be useful for streaming large files -
StAX event-based pull-style parsing can be useful for streaming large files requires stax.jar
DOM4J nicer syntax over DOM processing plus can be useful for large files if you use prune capability requires dom4j.jar
XOM nicer syntax over DOM processing plus a strong emphasis on compliancy requires xom.jar
JDOM nicer syntax over DOM processing requires jdom.jar
XPath use XPath expressions requires Java 5+ or JAXP 1.3+

or xalan.jar or jaxen.jar
XSLT when your transformation is more easily expressed using XSLT than code -
XQuery when your transformation is more easily expressed using XQuery than other alternatives mxquery.jar or alternative

Creating new XML

The most commonly used approach for creating XML with Groovy is to use a builder, i.e. one of:

Technology When/Why to use Requirements
MarkupBuilder supports Groovy's builder pattern with XML/HTML -
StreamingMarkupBuilder for larger files -

Groovy also has some low-level helper classes you typically won't need to use directly but you may sometimes see in older examples of using XML with Groovy.

Technology When/Why to use Requirements
SAXBuilder support class when using SAX -
StreamingSAXBuilder streaming version of SAXBuilder -
DOMBuilder support class when using DOM -
StreamingDOMBuilder streaming version of DOMBuilder -

You can also use Java API's which support XML document creation:

Technology When/Why to use Requirements
DOM low-level creation mechanism -
JDOM if you are an existing JDOM user requires jdom.jar
DOM4J if you are an existing DOM4J user requires dom4j.jar
XOM if you are an existing XOM user requires xom.jar

Troubleshooting

Some of the following Jira issues may indicate solutions or workarounds to problems other users faced while using XML.

Issue Description
GROOVY-2126 ClassCastException when using java.util.prefs package

More Information

For some more discussion of the pro's and con's of your XML Processing options and some additional details, see the following books:

The following articles may also be of interest:

You may also be interested in how XML is applied in other parts of Groovy: