2010-01-22 52 views
10

雖然試圖解析在Groovy RSS提要,我發現使用通配符GPATH例如:什麼是Groovy的GPath表達式的完整語法?

def text = """ 
<data> 
    <common-tables> 
    <table name="address"/> 
    <table name="phone"/> 
    </common-tables> 

    <special-tables> 
    <table name="person"/> 
    </special-tables> 

    <other-tables> 
    <table name="business"/> 
    </other-tables> 
</data> 
""" 

def xml = new XmlParser().parse(new ByteArrayInputStream(text.getBytes())) 
def tables = xml.'**'.table.findAll{ it.parent().name() == 
"special-tables" || it.parent().name 

(從http://old.nabble.com/Q:-Avoiding-XPath---using-GPath-td19087210.html

它看起來像一個有趣的使用「擴點」運算符。在Groovy網站,書籍等上我找不到任何這方面的參考。

這是如何工作的,更重要的是,您如何發現此問題? GPath中有沒有任何XPath'Rosetta Stone'?

回答

18

那麼,像往常一樣,查找信息的最佳位置在Groovy源代碼中。
解析的結果是一個groovy.util.slurpersupport.GPathResult對象。

如果你看一下源(普通的Java文件),你將看到的getProperty(String)方法具有以下特殊運算符:

  • 「..」返回父
  • 用來訪問屬性
  • 正常節點訪問「*」返回所有充當深度第一循環
  • 孩子
  • 「**」,「@」。

就是這樣,目前沒有其他的魔術關鍵字。

+0

非常好,謝謝!我曾試着尋找源頭,但無法弄清楚在哪裏尋找。 –

3

所有這些字符串都被視爲屬性。他們中沒有一個實際上是操作員

呼叫路由通過GPathResult#getProperty,專門檢查Gizmo的答案中列出的運營商。

相關問題