Groovy系列:GPath

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Groovy系列:GPath相关的知识,希望对你有一定的参考价值。

  1. //GPath can be used to easily query XML-based documents or even Object Graphs.
  2. //We start with looking at an XML GPath example.
  3.  
  4. //Let's load the Grails Pocast RSS Feed first.
  5. println "new XmlSluper".center(75, '*')
  6. def feed = new XmlSlurper().parse('http://hansamann.podhost.de/rss')
  7. assert feed.getClass().name == 'groovy.util.slurpersupport.NodeChild'
  8.  
  9.  
  10. //Like using XPath expressions, we can now navigate the tre
  11. //This accesses the first iem element of the RSS channel:
  12. println "Access to node content".center(75, '*')
  13. println feed.channel.item[0].title
  14. println feed.channel.item[0]['title']
  15.  
  16.  
  17. //As in XPath, attributes are accessed with the @ syntax.
  18. println "Access to attribute content".center(75, '*')
  19. println feed.channel.item[0].enclosure[email protected]
  20. println feed.channel.item[0].enclosure['@url']
  21.  
  22. //Let's see how many podcasts whe have done so far.
  23. println "item element count".center(75, '*')
  24. println feed.channel.item.size()
  25.  
  26. //We can iterate over all items with this
  27. println "All titles below".center(75, '*')
  28. feed.channel.item.each { item ->
  29. println item.title
  30. }
  31.  
  32. //hier will ich die items nach Groovy Series filtern...
  33. //def groovySeries = feed.channel.item.findAll{ return (it.title.indexOf('Groovy Series')) ? true : false;} //wieso geht das nicht... println it.title geht
  34. //println "Found ${groovySeries.size()}"
  35.  
  36. //playing with methods of GPathResult. NodeChild is an extension of GPathResult
  37. assert feed.channel.item[0].enclosure[0].getClass().name == 'groovy.util.slurpersupport.NodeChild'
  38.  
  39. //to call the attributes method, we neet to macke sure that we are operating on a single Node, not a set
  40. feed.channel.item[0].enclosure[0].attributes().each{println it} //works
  41. try {
  42. feed.channel.item[0].enclosure.attributes().each{println it}
  43. } catch (MissingMethodException e)
  44. {
  45. assert feed.channel.item[0].enclosure.getClass().name == 'groovy.util.slurpersupport.NodeChildren'
  46. //above returns a NodeChildren object as it is a path expression for all enclosure tags of the first item tag!
  47. }
  48.  
  49. //let's print all children of an item.
  50. println "All children of item[0]".center(75, '*')
  51. feed.channel.item[0].children().each { println it.name() } //will print tag names contained in item: title, description, author, pubDate, guid, link, enclosure
  52. assert feed.channel.item[0].children().size() == 7
  53.  
  54. //Beispiel mit GPathResult->find / findAll
  55.  
  56.  
  57. //evtl. xmlslurper dazu verwenden ein xml-doc zu veraendern??

以上是关于Groovy系列:GPath的主要内容,如果未能解决你的问题,请参考以下文章

groovy系列-groovy比起java,有哪些地方写起来更舒服

Gradle系列学习:初识Gradle之Groovy基础Gradle从入门到实战 - Groovy基础

Groovy使用 SwingBuilder 构建 Swing 窗口 ( 创建一个 Swing 构造器 | 配置 Swing 窗口的一系列属性 | 设置 Swing 窗口显示 )

Gradle系列之构建脚本基础

elasticsearch系列java定义score

Jenkins系列-Pipeline语法全集