Revision: 5986
Updated Code
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('http://hansamann.podhost.de/rss') 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, '*') println feed.channel.item[0].title println feed.channel.item[0]['title'] //As in XPath, attributes are accessed with the @ syntax. println "Access to attribute content".center(75, '*') println feed.channel.item[0].enclosure.@url println feed.channel.item[0].enclosure['@url'] //Let's see how many podcasts whe have done so far. println "item element count".center(75, '*') println feed.channel.item.size() //We can iterate over all items with this println "All titles below".center(75, '*') feed.channel.item.each { item -> println item.title } //hier will ich die items nach Groovy Series filtern... //def groovySeries = feed.channel.item.findAll{ 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 feed.channel.item[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 feed.channel.item[0].enclosure[0].attributes().each{println it} //works try { feed.channel.item[0].enclosure.attributes().each{println it} } catch (MissingMethodException e) { assert feed.channel.item[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, '*') feed.channel.item[0].children().each { println it.name() } //will print tag names contained in item: title, description, author, pubDate, guid, link, enclosure assert feed.channel.item[0].children().size() == 7 //Beispiel mit GPathResult->find / findAll //evtl. xmlslurper dazu verwenden ein xml-doc zu veraendern??
Revision: 5985
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 18, 2008 16:28 by hansamann
Initial Code
initial
Initial URL
Initial Description
Initial Title
Groovy Series: GPath
Initial Tags
object, xml, groovy
Initial Language
Groovy