2014年5月14日 星期三

[ Groovy Doc ] GPath

來源自 這裡 
Preface: 
GPath is a path expression language integrated into Groovy which allows parts of nested structured data to be identified. In this sense, it has similar aims and scope as XPath does for XML. The two main places where you use GPath expressions is when dealing with nested POJOs or when dealing with XML. 

As an example, you can specify a path to an object or element of interest: 
* a.b.c -> for XML, yields all the <c> elements inside <b> inside <a>
* a.b.c -> all POJOs, yields the <c> properties for all the <b> properties of <a> (sort of like a.getB().getC() in JavaBeans)

For XML, you can also specify attributes, e.g.: 
* a["@href"] -> the href attribute of all the a elements
* a.'@href' -> an alternative way of expressing this
* a.@href -> an alternative way of expressing this when using XmlSlurper

Example: 
  1. def text = """  
  2. <characters>  
  3.   <props>  
  4.     <prop>dd</prop>  
  5.   </props>  
  6.   <character id="1" name="Wallace">  
  7.      <likes>cheese</likes>  
  8.   </character>  
  9.   <character id="2" name="Gromit">  
  10.     <likes>sleep</likes>  
  11.   </character>  
  12. </characters>  
  13. """  
  14.   
  15. def node = new XmlSlurper().parseText(text);  
  16.   
  17. assert node != null  
  18. assert node.children().size() == 3 //, "Children ${node.children()}"  
  19.   
  20. def characters = node.character  
  21. println "node:" + node.children().size()  
  22. println "characters:" + node.character.size()  
  23. for (c in characters) {  
  24.     println c['@name']  
  25. }  
  26.   
  27. assert characters.size() == 2  
  28. assert node.character.likes.size() == 2 //, "Likes ${node.character.likes}"  
  29.   
  30. // lets find Gromit  
  31. def gromit = node.character.find { it['@id'] == '2' }  
  32. assert gromit != null //, "Should have found Gromit!"  
  33. assert gromit['@name'] == "Gromit"  
  34.   
  35. // lets find what Wallace likes in 1 query  
  36. def answer = node.character.find { it['@id'] == '1' }.likes.text()  
  37. assert answer == "cheese"  
Outline: 
Accessing element as property 
  1. def  characters = node.character  
  2. def  gromit =  node.character[1]  
Accessing attributes 
  1. println gromit['@name']  
  2. println gromit.@name  
Accessing element body 
  1. println gromit.likes[0].text()  
  2. println node.text()  
If the element is a father node,it will print all children's text. 

Explore the DOM use children() and parent() 
  1. def characters = node.children()  
  2. for (c in characters) {  
  3.     println c.@name  
  4. }  
Find elements use expression 
  1. def gromit = node.character.find { it.@id == '2' }  
Supplement: 
Groovy Doc - Processing XML 
[ In Action ] Dynamic object orientation - Using power features

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...