This example assumes the following class is already on your CLASSPATH:
Here is an example of using XmlSlurper:
You can also parse XML documents using namespaces:
XMLSluper 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.
- 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: