2016-06-14 58 views
0

請告知如何查找和輸出cust_JiraTaskId。我需要基於內部節點的最大數量的cust_JiraTaskId的值。在這個例子中它將是111111.如何找到價值與路徑x Groovy

我設法找到最大externalCode,現在我需要cust_JiraTaskId值。

<SFOData.cust_JiraReplication> 
<cust_HRISId>J000009</cust_HRISId> 
<externalCode>7</externalCode> 
<cust_JiraTask> 
    <externalCode>3</externalCode> 
    <cust_JiraTaskId>12345</cust_JiraTaskId> 
</cust_JiraTask> 
<cust_JiraTask> 
    <externalCode>5</externalCode> 
    <cust_JiraTaskId>111111</cust_JiraTaskId> 
</cust_JiraTask> 
</SFOData.cust_JiraReplication> 

我的劇本是

// Create an XPath statement to search for the 
element or elements you care   about: 
XPath x; 
x = XPath.newInstance("//cust_JiraTask/externalCode"); 
myElements = x.selectNodes(doc); 
String maxvalue = ""; 
for (Element myElement : myElements) { 
if (myElement.getValue() > maxvalue) 
{ 
    maxvalue = myElement.getValue(); 
} 
} 
props.setProperty("document.dynamic.userdefined.externalCode", maxvalue); 

感謝幫助下。

回答

0

這對我的作品使用Groovy 2.4.5:

def xml = """ 
<SFOData.cust_JiraReplication> 
<cust_HRISId>J000009</cust_HRISId> 
<externalCode>7</externalCode> 
<cust_JiraTask> 
    <externalCode>3</externalCode> 
    <cust_JiraTaskId>12345</cust_JiraTaskId> 
</cust_JiraTask> 
<cust_JiraTask> 
    <externalCode>5</externalCode> 
    <cust_JiraTaskId>111111</cust_JiraTaskId> 
</cust_JiraTask> 
</SFOData.cust_JiraReplication> 
""" 

def xs = new XmlSlurper().parseText(xml) 

def nodes = xs.cust_JiraTask.cust_JiraTaskId 

def maxNode = nodes.max { it.text() as int } 
assert 111111 == maxNode.text() as int