Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 20 Next »

GPath is a path expression language integrated into Groovy which allows parts of nested structured data to be identified. In this sense, it has similar aims and scope as XPath does for XML. The two main places where you use GPath expressions is when dealing with nested POJOs or when dealing with XML.

As an example, you can specify a path to an object or element of interest:
a.b.c -> for XML, yields all the <c> elements inside <b> inside <a>
a.b.c -> all POJOs, yields the <c> properties for all the <b> properties of <a> (sort of like a.getB().getC() in JavaBeans)

For XML, you can also specify attributes, e.g.:
a["@href"] -> the href attribute of all the a elements
a.'@href' -> an alternative way of expressing this
a.@href -> an alternative way of expressing this when using XmlSlurper

Example

The best example of GPath for xml is test-new/groovy/util/XmlSlurperTest.groovy.

Outline

1.Accessing element as property
2.Accessing attributes
3.Accessing element body

If the element is a father node,it will print all children's text.

3.Explore the DOM use children() and parent()
4.Find elements use expression

Another Example

Here is a two line example of how to get a list of all the links to .xml files listed on a web page. The Neko parser is used to parse non-well formed html; it ships as part of the standard Groovy distribution.

More Information

See also: Processing XML

  • No labels