需要使用Groovy更新Tag值

时间:2014-08-18 13:25:59

标签: groovy

我需要将CATEGORY的TRDCHARGE_AMOUNT的标记值更新为TEST。我已经能够更新其他直接标记的值。

<?xml version="1.0" encoding="UTF-8"?>
<TRANSACTIONS ASOF_DATE="4/2/2014" CREATE_DATE="4/2/2014" RECORDS="1">
    <TRADE>
        <CUSIP>31384WPS3</CUSIP>
        <DESC_INSTMT>CREDIT INDUSTRIEL ET COMMERCIAL (N</DESC_INSTMT>
        <DESK>a</DESK>
        <DESK_TYPE>GEN</DESK_TYPE>
        <DTM_2A7>7</DTM_2A7>
        <EXECUTION_TIME>4/1/2014 10:03:06.000</EXECUTION_TIME>
        <EXEC_TIME_SOURCE>A</EXEC_TIME_SOURCE>
        <FUND>VIMVP-FI</FUND>
        <INT_AT_MATURITY>536.6666666667</INT_AT_MATURITY>
        <INVNUM>-911223</INVNUM>
        <TRDCHARGE_set SIZE="2">
      - <TRDCHARGE>
          <CALC_TYPE>FLAT</CALC_TYPE> 
          <CATEGORY>TEST</CATEGORY> 
          <RATE>0.0000000000</RATE> 
          <TRDCHARGE_AMOUNT>1000.0000000000</TRDCHARGE_AMOUNT> 
          <TRDCHARGE_SCHEDULE_ID>-111111.0000000000</TRDCHARGE_SCHEDULE_ID> 
      </TRDCHARGE>
      - <TRDCHARGE>
          <CALC_TYPE>FLAT</CALC_TYPE> 
          <CATEGORY>LOCL</CATEGORY> 
          <RATE>0.0000000000</RATE> 
          <TRDCHARGE_AMOUNT>50.0000000000</TRDCHARGE_AMOUNT> 
          <TRDCHARGE_SCHEDULE_ID>-112221.0000000000</TRDCHARGE_SCHEDULE_ID> 
      </TRDCHARGE>
    </TRDCHARGE_set>
    </TRADE>

我使用了以下代码,但不知道如何在TRDCHARGE标记下更新TRDAMOUNt。

new XmlParser().parse(xmlFile).with { x ->
    x.TRADE.each { aa ->
        aa.children().find { it -> it.name() == 'FUND' }?.value = data[0]
        aa.children().find { it -> it.name() == 'INVNUM' }?.value = data[1]
        aa.children().find { it -> it.name() == 'CUSIP' }?.value = data[2]      
        aa.children().find { it -> it.name() == 'TRAN_TYPE' }?.value = data[3]
        aa.children().find { it -> it.name() == 'SETTLE_EXCH_RATE' }?.value = data[4]
        aa.children().find { it -> it.name() == 'TRD_ORIG_FACE' }?.value = data[5]      
        aa.children().find { it -> it.name() == 'TRD_PRINCIPAL' }?.value = data[6]           
    }
    def dir = "D:\\Invesco\\Alladin\\Test Data Files\\Input Files Through SoapUi\\"
    def xFile1 = "Transaction"+data[1]+".xml"
    def xmlFile1 = dir+xFile1
    def printer = new XmlNodePrinter(new PrintWriter(xmlFile1))
    printer.preserveWhitespace = true
    printer.print(x)
}

1 个答案:

答案 0 :(得分:0)

您可以这样做:

import groovy.xml.XmlUtil

def example = '''
<TRANSACTIONS ASOF_DATE="4/2/2014" CREATE_DATE="4/2/2014" RECORDS="1">
  <TRADE>
    <CUSIP>31384WPS3</CUSIP>
    <DESC_INSTMT>CREDIT INDUSTRIEL ET COMMERCIAL (N</DESC_INSTMT>
    <DESK>a</DESK>
    <DESK_TYPE>GEN</DESK_TYPE>
    <DTM_2A7>7</DTM_2A7>
    <EXECUTION_TIME>4/1/2014 10:03:06.000</EXECUTION_TIME>
    <EXEC_TIME_SOURCE>A</EXEC_TIME_SOURCE>
    <FUND>VIMVP-FI</FUND>
    <INT_AT_MATURITY>536.6666666667</INT_AT_MATURITY>
    <INVNUM>-911223</INVNUM>
  <TRDCHARGE_set SIZE="2">
  <TRDCHARGE>
      <CALC_TYPE>FLAT</CALC_TYPE>
      <CATEGORY>TEST</CATEGORY>
      <RATE>0.0000000000</RATE>
      <TRDCHARGE_AMOUNT>1000.0000000000</TRDCHARGE_AMOUNT>
      <TRDCHARGE_SCHEDULE_ID>-111111.0000000000</TRDCHARGE_SCHEDULE_ID>
  </TRDCHARGE>
  <TRDCHARGE>
      <CALC_TYPE>FLAT</CALC_TYPE>
      <CATEGORY>LOCL</CATEGORY>
      <RATE>0.0000000000</RATE>
      <TRDCHARGE_AMOUNT>50.0000000000</TRDCHARGE_AMOUNT>
      <TRDCHARGE_SCHEDULE_ID>-112221.0000000000</TRDCHARGE_SCHEDULE_ID>
  </TRDCHARGE>
 </TRDCHARGE_set>
 </TRADE>
</TRANSACTIONS>
'''

new XmlParser().parseText(example).with { x ->
  x.TRADE.each { trade ->
    trade.children().find { it -> it.name() == 'FUND' }?.value = 'CASH'
    trade.children().find { it -> it.name() == 'INVNUM' }?.value = '-675'
    trade.children().find { it -> it.name() == 'CUSIP' }?.value = '3B56GTUNN'
    trade.TRDCHARGE_set.TRDCHARGE.each {
        it.children().find { it.name() == 'TRDCHARGE_AMOUNT'}?.value = 5.0
    }
  }
  println XmlUtil.serialize(x)
}

这会将所有TRDCHARGE_AMOUNT值更改为5.0并打印结果。