2016-03-15 67 views
0

我的XML返回基於條件的另一個節點是這樣的:在常規

<app> 
    <module> 
     <web> 
     <web-uri>abc.war</web-uri> 
     <context-root>abc</context-root> 
     </web> 
    </module> 
    <module> 
     <web> 
     <web-uri>abc.pl</web-uri> 
     <context-root>adf</context-root> 
     </web> 
    </module> 
</app> 

我需要的context-root值只有web-uri值結束與戰爭。作爲一個列表。

我寫的是這樣的:

def ctxRootList = nodeList.'**'.findAll{ 
     ctxRoot -> 
        ctxRoot.module.web."web-uri".text().endsWith(".war") 
    }*.text() 

有2個問題在這裏:

  1. 我不知道如何讓其他的兄弟姐妹,而在web-uri
  2. 結果現在似乎作爲所有這些值的連接字符串返回。那麼,如何讓它返回作爲值

回答

1

列表在這裏你去:

def txt = ''' 
<app> 
    <module> 
      <web> 
       <web-uri>abc.war</web-uri> 
       <context-root>abc</context-root> 
      </web> 
     </module> 
     <module> 
      <web> 
       <web-uri>abc.pl</web-uri> 
       <context-root>adf</context-root> 
      </web> 
    </module> 
</app>''' 

def xml = new XmlSlurper().parseText(txt) 
xml.module.web.findAll { it.'web-uri'.text().endsWith('war') }*.'context-root'*.text() 

訣竅是尋找web元素 - 既web-uricontext-root的母公司。如果找到這樣的元素,你可以很容易地引用它的孩子。

+0

爲什麼在上下文之後的另一顆星 - 根? – Vik

+0

@Vik,因爲可能有多個'context-root'元素,所以'text()'在每個找到的元素上被調用。 – Opal