如何使用路径x Groovy查找值

时间:2016-06-14 12:10:50

标签: xpath groovy

请告知如何查找和输出cust_JiraTaskId。我需要基于内部节点的最大数量的cust_JiraTaskId的值。在这个例子中,它将是111111。

我设法找到了max 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);

感谢您的帮助。

1 个答案:

答案 0 :(得分: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