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 9 Next »

GPath is a path expression language we've integrated into Groovy which is similar in aims and scope as XPath is to XML.
1. Expression language for tree structured data
2. Implementations for XML
3. Can specify a path to an element
a.b.c -> all the <c> elements inside <b> inside <a>
4. Can specify attributes
a"@href" -> the href attribute of all the a elements


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

Error rendering macro 'code': Invalid value specified for parameter 'lang'
package org.codehaus.groovy.sandbox.util

class XmlSlurperTest extends GroovyTestCase {
void testXmlParser() {
    text = """
  <character id="1" name="Wallace">
  <character id="2" name="Gromit">
    node = new XmlSlurper().parseText(text);
    assert node != null
    assert node.children().size() == 3 //, "Children ${node.children()}"
    characters = node.character
    println "node:" + node.children().size()
    println "charcters:" + node.character.size()
    for (c in characters) {
        println c['@name']
    assert characters.size() == 2
    assert node.character.likes.size() == 2 //, "Likes ${node.character.likes}"
    // lets find Gromit
    gromit = node.character.find { it['@id'] == '2' }
    assert gromit != null //, "Should have found Gromit!"
    assert gromit['@name'] == "Gromit"        
    // lets find what Wallace likes in 1 query
    answer = node.character.find { it['@id'] == '1' }.likes[0].text()
    assert answer == "cheese"


Error rendering macro 'link' : Link needs a name and a URL as arguments.
by Jack Herrington.
Error rendering macro 'link' : Link needs a name and a URL as arguments.

  • No labels