0

我必须计算出现在 XML 中的每个无效数字 (BTNumber) 的出现次数。我正在使用 groovy XmlSlurper() 。请建议如何计算数字和计数(在 xml 中的多次)

例如-

   BTNumber     | count
 ----------------------------------
   2205867438   |   2
   78124614     |   3

XML代码如下 -

<Results>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>2205867438
</BTNumber>
    <CPNumber>0000000000</CPNumber>
    <DialedNumber>2461253052</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>customer1</CarrierCode>
    <DateStamp>2013-03-24 11:50:04.0</DateStamp>
</Row>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>2205867438</BTNumber>
    <CPNumber>0000000000</CPNumber>
    <DialedNumber>2461253052</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>customer1</CarrierCode>
    <DateStamp>2013-03-24 11:50:18.0</DateStamp>
</Row>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>2201159526</BTNumber>
    <CPNumber>0000000000</CPNumber>
    <DialedNumber>1111253052</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>customer1</CarrierCode>
    <DateStamp>2013-03-24 11:53:07.0</DateStamp>
</Row>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>1011159526</BTNumber>
    <CPNumber>0000000000</CPNumber>
    <DialedNumber>1111253052</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>customer1</CarrierCode>
    <DateStamp>2013-03-24 11:53:18.0</DateStamp>
</Row>
</Results>
 <Results>
  <Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>78124614</BTNumber>
    <CPNumber>78124614</CPNumber>
    <DialedNumber>7812461400</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>MyNetwork</CarrierCode>
    <DateStamp>2013-03-24 10:18:50.0</DateStamp>
</Row>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>78124614</BTNumber>
    <CPNumber>78124614</CPNumber>
    <DialedNumber>7812461400</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>MyNetwork</CarrierCode>
    <DateStamp>2013-03-24 15:35:31.0</DateStamp>
</Row>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>78124614</BTNumber>
    <CPNumber>78124614</CPNumber>
    <DialedNumber>7812461400</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>MyNetwork</CarrierCode>
    <DateStamp>2013-03-24 15:36:59.0</DateStamp>
</Row>
 </Results>

请建议是否有比 XMLSlurper 更容易和更快的方法。

4

2 回答 2

1

首先,您的 xml 重复了<Results>. 你需要那个吗?

您可以使用以下内容来获取唯一id且对应的地图count

def xmlObj = new XmlSlurper().parseText(xml)
def btNumbers = xmlObj.Row.collect{it.BTNumber} 
def countMap = btNumbers.unique(false).collectEntries{btNumber-> 
    [btNumber, btNumbers.count(btNumber)]
}

xml您提到的 xml 元素在哪里。

<Results>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>2205867438</BTNumber>
    <CPNumber>0000000000</CPNumber>
    <DialedNumber>2461253052</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>customer1</CarrierCode>
    <DateStamp>2013-03-24 11:50:04.0</DateStamp>
</Row>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>2205867438</BTNumber>
    <CPNumber>0000000000</CPNumber>
    <DialedNumber>2461253052</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>customer1</CarrierCode>
    <DateStamp>2013-03-24 11:50:18.0</DateStamp>
</Row>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>2201159526</BTNumber>
    <CPNumber>0000000000</CPNumber>
    <DialedNumber>1111253052</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>customer1</CarrierCode>
    <DateStamp>2013-03-24 11:53:07.0</DateStamp>
</Row>
<Row>
    <DialedCountry>001</DialedCountry>
    <BTNumber>1011159526</BTNumber>
    <CPNumber>0000000000</CPNumber>
    <DialedNumber>1111253052</DialedNumber>
    <Wireless>1</Wireless>
    <CarrierCode>customer1</CarrierCode>
    <DateStamp>2013-03-24 11:53:18.0</DateStamp>
</Row>
</Results>
于 2013-05-29T20:00:39.133 回答
0

假设有一个额外的数量字段,那么在这个 xml 中检查类似的 BTnumbers 值并在 groovy 中添加相应的 QTY 值的理想方法是什么。

于 2019-08-23T03:41:21.083 回答