需要在Groovy中为Tag名称设置XML标记值

时间:2014-07-14 16:05:30

标签: groovy xml-parsing

我需要为下面的XML设置一些标签值。这些标记名称及其值即将出现csv文件。

def temp1 = """
<ns2:DTLTrade xmlns:ns2="http://Invesco.DTL.Common.EMM.Schemas.Trade" xmlns:ns1="http://invesco.dtl.common.schemas" xmlns:ns0="Trade" xmlns:ns3="http://Invesco.DTL.Common.EMM.Schemas.ExtensionSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <ns0:Trade xsi:type="ns0:Trade" Prin_Local_Ccy="USD" Trade_Quantity="450.00000000000000" Trade_Status_Code="New" Instrument_ID="2663" Trade_Date="11/4/2014 00:00:00" Settl_Date="01/09/2014 00:00:00" Buy_Sell_Ind="B" Source_System_ID="CRD" Agency_Principal_Ind="A" Cancel_Reason="RESEND" Long_Short_Ind="L" Unit_Price_Local_Ccy="94.98000000000000" IPO_Indicator="False" Prin_Settl_Ccy="GBP" Prin_Local_To_Base_Ccy_Fx_Rate="1.00000000000000" Inc_Local_To_Base_Ccy_Fx_Rate="0.00000000000000" Prin_Local_To_Sett_Ccy_Fx_Rate="1.00000000000000" Inc_Local_To_Sett_Ccy_Fx_Rate="" Trade_Touch_Count="0.00" Portfolio_ID="V-0933572">
   <ns0:Trade_Counterparty Trade_ID="38161219" Legal_Entity_Role_Code="EXECUTION BROKER" Trade_Counterparty_ID="ABEL_US_EQAT" /> 
   <ns0:Trade_Counterparty Trade_ID="38161219" Legal_Entity_Role_Code="STEP OUT BROKER" Trade_Counterparty_ID="ABGB_UK_CSHL" /> 
   <ns0:Trade_Amount Trade_ID="38161219" Amount_Type="Net Cash Base" Amount_Ccy="USD" Amount="3000" /> 
   <ns0:Trade_Amount Trade_ID="38161219" Amount_Type="Execution Amount" Amount_Ccy="USD" Amount="1000" /> 
   <ns0:Trade_Amount Trade_ID="38161219" Amount_Type="Net Cash Local" Amount_Ccy="USD" Amount="2000" /> 
   <ns0:Trade_Amount Trade_ID="38161219" Amount_Type="Settlement Amount" Amount_Ccy="GBP" Amount="5000" /> 
   <ns0:Trade_Amount Trade_ID="38161219" Amount_Type="Traded_Interest_Local" Amount_Ccy="" Amount="0.0000" /> 
   <ns0:Trade_Amount Trade_ID="38161219" Amount_Type="Traded_Principal_Amount" Amount_Ccy="USD" Amount="1000" /> 
   <ns0:Trade_Employee Trade_ID="38161219" Employee_ID="BROWNEP1" Employee_Role="Trader" /> 
   <ns0:Trade_Employee Trade_ID="38161219" Employee_ID="PAEZL" Employee_Role="Manager" /> 
   <ns0:Trade_Charge Trade_ID="38161219" Charge_Code="Commission" Trade_Charge_ID="1" Charge_Amt_Loc_Ccy="10.0000" /> 
   <ns0:Trade_Charge Trade_ID="38161219" Charge_Code="CE" Trade_Charge_ID="1" Charge_Amt_Loc_Ccy="11.0000" /> 
   <ns0:Trade_Charge Trade_ID="38161219" Charge_Code="CF" Trade_Charge_ID="1" Charge_Amt_Loc_Ccy="12.0000" /> 
   <ns0:Trade_Charge Trade_ID="38161219" Charge_Code="FR" Trade_Charge_ID="1" Charge_Amt_Loc_Ccy="13.0000" /> 
   <ns0:Trade_Charge Trade_ID="38161219" Charge_Code="FTR" Trade_Charge_ID="1" Charge_Amt_Loc_Ccy="14.0000" /> 
   <ns0:Trade_Charge Trade_ID="38161219" Charge_Code="GST" Trade_Charge_ID="1" Charge_Amt_Loc_Ccy="15.0000" /> 
   <ns0:Trade_Charge Trade_ID="38161219" Charge_Code="LOCL" Trade_Charge_ID="1" Charge_Amt_Loc_Ccy="16.0000" /> 
   <ns0:Trade_System_Reference System_ID="CRD" Trade_ID="38161219" Trade_Ref_ID="38161219" /> 
   <ns1:Header>
      <SourceId>CRD</SourceId> 
      <Status>New</Status> 
      <MessageId>38161219</MessageId> 
      <Version>1.0.0</Version> 
   </ns1:Header>
   </ns0:Trade>
</ns2:DTLTrade>
"""

我使用下面的代码,但在这里我必须硬编码值 -

def xml = new XmlParser(false, false).parseText(temp1)
xml.'**'.'ns0:Trade'.each {
    it.@Cancel_Reason = "100"
}

0 个答案:

没有答案