Reading XML with Groovy and XPath Add comment to Wiki View in Wiki Edit Wiki page Printable Version

Car Example

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


Using Xalan

Here is an example of using Apache Xalan with Groovy to read an existing XML file:

Using Jaxen

Here is an example of using Jaxen with Groovy to read an existing XML file:

Note: many libraries (e.g. DOM4J, JDOM, XOM) bundle or provide optional support for Jaxen. You may not need to download any additional JARs to use it.

Using Java's native XPath support

If you are using Java 5 and above, you don't need to use the standalone Xalan jar but instead can use the now built-in XPath facilities:

People in Groups Example

Inspired by this example, here is how to use XPath to determine which groups include all three of alan, paul and ivan for this graph representing group membership.

Picture source:

First, here is the XML data capturing the group membership information:

Using Java's native XPath support

Here is how to check that groups 2 and 4 are the groups in question using the built-in XPath capabilities of Java 5 and above:

And you can refactor your XPath Expression with something like:

Using GPath

Of course, you can also do this without XPath by using Groovy's GPath facilities as follows:



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.

  By  -  pages  -  views  - last modified