Return to Snippet

Revision: 5986
at April 18, 2008 19:24 by hansamann

Updated Code
//GPath can be used to easily query XML-based documents or even Object Graphs. 
//We start with looking at an XML GPath example.

//Let's load the Grails Pocast RSS Feed first.
println "new XmlSluper".center(75, '*')
def feed = new XmlSlurper().parse('')
assert feed.getClass().name == 'groovy.util.slurpersupport.NodeChild'

//Like using XPath expressions, we can now navigate the tre
//This accesses the first iem element of the RSS channel:
println "Access to node content".center(75, '*')

//As in XPath, attributes are accessed with the @ syntax.
println "Access to attribute content".center(75, '*')
println[0][email protected] 

//Let's see how many podcasts whe have done so far. 
println "item element count".center(75, '*')

//We can iterate over all items with this
println "All titles below".center(75, '*') { item ->
    println item.title

//hier will ich die items nach Groovy Series filtern... 
//def groovySeries ={ return (it.title.indexOf('Groovy Series')) ? true : false;} //wieso geht das nicht... println it.title geht
//println "Found ${groovySeries.size()}"

//playing with methods of GPathResult. NodeChild is an extension of GPathResult
assert[0].enclosure[0].getClass().name == 'groovy.util.slurpersupport.NodeChild'

//to call the attributes method, we neet to macke sure that we are operating on a single Node, not a set[0].enclosure[0].attributes().each{println it} //works
try {[0].enclosure.attributes().each{println it}
} catch (MissingMethodException e)
    assert[0].enclosure.getClass().name == 'groovy.util.slurpersupport.NodeChildren'
    //above returns a NodeChildren object as it is a path expression for all enclosure tags of the first item tag!

//let's print all children of an item.
println "All children of item[0]".center(75, '*')[0].children().each { println } //will print tag names contained in item: title, description, author, pubDate, guid, link, enclosure
assert[0].children().size() == 7

//Beispiel mit GPathResult->find / findAll

//evtl. xmlslurper dazu verwenden ein xml-doc zu veraendern??

Revision: 5985
at April 18, 2008 16:28 by hansamann

Initial Code

Initial URL

Initial Description

Initial Title
Groovy Series: GPath

Initial Tags
object, xml, groovy

Initial Language