Processing existing XML
Groovy provides special XML processing support through the following classes:
Technology |
When/Why to use |
Requirements |
|---|---|---|
supports GPath expressions for XML documents and allows updating |
- |
|
lower overheads than XmlParser due to lazy evaluation but only supports read operations |
- |
|
low-level tree-based processing where you want some syntactic sugar |
place |
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 |
|---|---|---|
low-level tree-based processing |
- |
|
event-based push-style parsing can be useful for streaming large files |
- |
|
event-based pull-style parsing can be useful for streaming large files |
requires stax.jar |
|
nicer syntax over DOM processing plus can be useful for large files if you use prune capability |
requires dom4j.jar |
|
nicer syntax over DOM processing plus a strong emphasis on compliancy |
requires xom.jar |
|
nicer syntax over DOM processing |
requires jdom.jar |
|
use XPath expressions |
requires xml-apis.jar |
|
use XPath expressions with slightly more efficiency than built-in XPath |
requires jaxen.jar |
Creating new XML
The most used Groovy technique is to use one of:
Technology |
When/Why to use |
Requirements |
|---|---|---|
MarkupBuilder |
supports Groovy's builder pattern with XML/HTML |
- |
StreamingMarkupWriter |
for larger files |
- |
You can also use XmlParser, DOM, DOM with category support, DOM4J, JDOM.
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:
- Chapter 12 of GINA
- Processing XML with Java
- Java and XML
The following articles may also be of interest:
- XML and Java technologies: Document models, Part 1: Performance
- DOM, DOM4J, JDOM, XOM Comparison (slightly outdated)
- Dom4J performance versus Xerces / Xalan
You may also be interested in how XML is applied in other parts of Groovy: