Quick page lookup

×
Go!

★ Groovy

  • Documentation
    • Documentation Portal
    • Installing Groovy
    • Getting Started
    • User Guide
    • Testing Guide
    • Advanced Usage Guide
    • Developer Guide
    • Cookbook Examples
    • Groovy APIs
    • Groovy Development Kit
  • Community & Support
    • Welcome!
    • Groovy Sources
    • Mailing Lists
    • Bug Tracker
    • Wiki
    • Contributing
    • Related Projects
    • Modules
    • User Groups
  • IDE Support
    • Eclipse Plugin
    • IntelliJ IDEA Plugin
    • Other IDE plugins
  • Download

Reading XML using Groovy's XmlSlurper

This example assumes the following class is already on your CLASSPATH:

XmlExamples.groovy

Here is an example of using XmlSlurper:

Notes:

  • If your elements contain characters such as dashes, you can enclose the element name in double quotes. Meaning for:
  • You do the following:





You can also parse XML documents using namespaces:

XmlSlurper has a declareNamespace method which takes a Map of prefix to URI mappings. You declare the namespaces and just use the prefixes in the GPath expression.

Some remarks:

  • name or "*:name" matches an element named "name" irrespective of the namespace it's in (i.e. this is the default mode of operation)
  • ":name" matches an element named "name" only id the element is not in a namespace
  • "prefix:name" matches an element names "name" only if it is in the namespace identified by the prefix "prefix" (and the prefix to namespace mapping was defined by a previous call to declareNamespace)

You can generate namespaced elements in StreamingMarkupBuilder very easily:

 

Search

Results of your search request can come from various sources: the Groovy website itself, the JIRA issues, the API documentation, as well as a few other interesting Groovy-related blogs.

spacer
spacer  By  -  pages  -  views  - last modified 
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.