在xml解析中更改属性值时发出问题

时间:2014-06-25 10:38:44

标签: groovy

我想从下面创建一个新的xml,除了一些字段的值,例如Issue_Price和Issue_Currency_code,它取一些csv的值。

def temp="""
<ns0:DTLSecurity xmlns:ns0="http://Invesco.DTL.Common.EMM.Schemas.Security">
  <ns1:Instrument_Issue Sec_ID="31" Issue_Amount="51" Issue_Price="92" Issue_Date="1999-    05-31" Issue_Currency_code="Iss"     Issue_Name_Short="Issue_Name_ShortIssue_Name_ShortIssue_Name_ShortIssue_Name_ShortIssue_Name    _ShortIssue_Name_ShortIssue_Name_ShortIssue_Name_ShortIssue_Name_ShortIssue_Name_ShortIssue_    Name_ShortIssue_Name_ShortIssue_Name_ShortIssue_Name_ShortIssue_Name_ShortIssue_Name_Shor"     Issue_Name_Long="Issue_Name_LongIssue_Name_LongIssue_Name_LongIssue_Name_LongIssue_Name_Long    Issue_Name_LongIssue_Name_LongIssue_Name_LongIssue_Name_LongIssue_Name_LongIssue_Name_LongIs    sue_Name_LongIssue_Name_LongIssue_Name_LongIssue_Name_LongIssue_Name_LongIssue_Name_Long"     Issue_Desc_Short="Issue_Desc_ShortIssue_Desc_ShortIssue_Desc_ShortIs"     Issue_Desc="Issue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_Desc    Issue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIs    sue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssu    e_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_DescIssue_    DescIssue_DescIssue_DescIssue_DescIssue_Desc"     Issue_Name_Chinese_Tradl="Issue_Name_Chinese_TradlIssue_Name_Chinese_TradlIssue_Name_Chinese    _TradlIssue_Name_Chinese_TradlIssue_Name_Chinese_TradlIssue_Name_Chinese_TradlIssue_Name_Chi    nese_TradlIssue_Name_Chinese_TradlIssue_Name_Chinese_TradlIssue_Name_Chinese_TradlIssue_Name    _Chinese_TradlIssue_Name_Chinese_TradlIssue_Name_Chinese_TradlIssue_Name_Chinese_TradlIssue_    Name_Chinese_TradlIssue_Name_Chinese_TradlIssue_Name_Chine"     Issue_Name_Chinese_Simpl="Issue_Name_Chinese_SimplIssue_Name_Chinese_SimplIssue_Name_Chinese    _SimplIssue_Name_Chinese_SimplIssue_Name_Chinese_SimplIssue_Name_Chinese_SimplIssue_Name_Chi    nese_SimplIssue_Name_Chinese_SimplIssue_Name_Chinese_SimplIssue_Name_Chinese_SimplIssue_Name    _Chinese_SimplIssue_Name_Chinese_SimplIssue_Name_Chinese_SimplIssue_Name_Chinese_SimplIssue_    Name_Chinese_SimplIssue_Name_Chinese_SimplIssue_Name_Chine"     Issue_Market="Issue_MarketIssue_MarketIssue_MarketIssue_MarketIs" Issue_Country_Code="Iss"     Maturity_Date="1999-05-31" Class="ClassClassClassClass" Series="SeriesSe" Sec_Reg_S_Ind="S"     Sec_12D1_Ind="Se" Sec_12D3_Ind="Se" Sec_144A_Ind="Se" Sec_Red_D_Ind="Se"     Accrual_Type_Code="Accrual_Type_CodeAcc" Aggte_Amt_Issued_Flag="A" Issue_Outstnd_Amount="39"     Alt_Min_Tax_Flag="A" Avz_Restric_Ind="A" Bank_Qualif_Ind="B" Dividend_Currency_Code="Div"     Default_Date="1999-05-31" Default_Type="De" PAR_Amount="58" Bearer_Ind="B"     PAR_Amount_Ccy="PAR" Blended_Sec_Ind="B" Bullet_Ind="B" Illiquid_Ind="I"     Processed_Date="1999-05-31" Perpetual_Ind="P" PIK_Ind="P"     Sec_Tax_Code="Sec_Tax_CodeSec_Tax_CodeSec_Tax_CodeSec_Tax_CodeSe"     Sec_Tax_Provision="Sec_Tax_ProvisionSec_Tax_ProvisionSec_Tax_ProvisionSec_Tax_ProvisionSec_T    ax_ProvisionSec_Tax_Provisi"     Offering_Type="Offering_TypeOffering_TypeOffering_TypeOffering_TypeOffering_TypeOffering_Typ    eOffering_TypeOffering_TypeOffering_TypeOffering_TypeOffering_TypeOffering_TypeOffering_Type    Offering_Ty" Puttable_Ind="P" Partial_Paymt_Ind="P" Poison_Pill_Ind="P"     Poison_Pill_Date="1999-05-31" Poison_Pill_Expire_Date="1999-05-31"     Poison_Pill_Exrs_Price="19" Pre_Refunded_Price="0" Pre_Refunded_Date="1999-05-31"     Tier="TierTierTi" Unitized_Ind="Un" FAS157_Default_Level="FAS157_Default_Level" Factor="4"     Factorable_Ind="Fa" Simpl_Sec_Status="Simpl_Sec_StatusSimp"     Delivery_Type="Delivery_TypeDeliver" DTC_Eligible_Ind="DT" Dual_Currency_Ind="Du"     Fix_Float_Code="Fix" Contract_Size="71" Collateral_Type="Collateral_TypeColla"     Collateral_Amt="31" Contract_Increment="10.4" Extendable_Ind="Ex" Last_Update_Date="1999-05-    31T13:20:00.000-05:00" Expiration_Date="1999-05-31" xmlns:ns1="Security">
    <ns1:Instrument_Listing Instrument_Listing_Id="100" Trading_Venue_ID="100"         Lot_Size="24" Lot_Size_Unit="21" Status="StatusStat" Sec_ID="85" Cadis_Id="100"     Ticker="TickerTickerTickerTickerTicker" Currency_Code="Cur">
      <ns1:Instrument_Identifier Instrument_Listing_Id="100"     Identifier_Type="Identifier_TypeIdentifier_Type"     Instrument_Identifier="Instrument_IdentifierInstrument_IdentifierInstrument_IdentifierInstru    ment_IdentifierInstrument_Ident" Active_Ind="A" />
    </ns1:Instrument_Listing>
    <ns1:Instrument_Listing Instrument_Listing_Id="100" Trading_Venue_ID="100"     Lot_Size="16" Lot_Size_Unit="19" Status="StatusStat" Sec_ID="87" Cadis_Id="100"     Ticker="TickerTickerTickerTickerTicker" Currency_Code="Cur">
      <ns1:Instrument_Identifier Instrument_Listing_Id="100"     Identifier_Type="Identifier_TypeIdentifier_Type"     Instrument_Identifier="Instrument_IdentifierInstrument_IdentifierInstrument_IdentifierInstru    ment_IdentifierInstrument_Ident" Active_Ind="A" />
    </ns1:Instrument_Listing>
    <ns1:Instrument_Listing Instrument_Listing_Id="100" Trading_Venue_ID="100"     Lot_Size="66" Lot_Size_Unit="27" Status="StatusStat" Sec_ID="93" Cadis_Id="100"     Ticker="TickerTickerTickerTickerTicker" Currency_Code="Cur">
      <ns1:Instrument_Identifier Instrument_Listing_Id="100"     Identifier_Type="Identifier_TypeIdentifier_Type"     Instrument_Identifier="Instrument_IdentifierInstrument_IdentifierInstrument_IdentifierInstru    ment_IdentifierInstrument_Ident" Active_Ind="A" />
    </ns1:Instrument_Listing>
    <ns1:Instrument_Organisation Instrument_Org_ID="100" Legal_Entity_ID="100"     Legal_Entity_Role_ID="100" Sec_ID="76" />
    <ns1:Instrument_Organisation Instrument_Org_ID="100" Legal_Entity_ID="100"     Legal_Entity_Role_ID="100" Sec_ID="22" />
    <ns1:Instrument_Organisation Instrument_Org_ID="100" Legal_Entity_ID="100"     Legal_Entity_Role_ID="100" Sec_ID="77" />
  </ns1:Instrument_Issue>
  <ns2:Header xmlns:ns2="http://invesco.dtl.common.schemas">
    <SourceId>SourceId_0</SourceId>
    <Status>Status_0</Status>
    <MessageId>MessageId_0</MessageId>
    <Version>Version_0</Version>
    <ns3:Extensions xmlns:ns3="http://Invesco.DTL.Common.EMM.Schemas.ExtensionSchema">
      <ExtensionItem Key="Key_0" Value="Value_1" />
      <ExtensionItem Key="Key_0" Value="Value_1" />
      <ExtensionItem Key="Key_0" Value="Value_1" />
    </ns3:Extensions>
    <ns3:Extensions xmlns:ns3="http://Invesco.DTL.Common.EMM.Schemas.ExtensionSchema">
      <ExtensionItem Key="Key_0" Value="Value_1" />
      <ExtensionItem Key="Key_0" Value="Value_1" />
      <ExtensionItem Key="Key_0" Value="Value_1" />
    </ns3:Extensions>
    <ns3:Extensions xmlns:ns3="http://Invesco.DTL.Common.EMM.Schemas.ExtensionSchema">
      <ExtensionItem Key="Key_0" Value="Value_1" />
      <ExtensionItem Key="Key_0" Value="Value_1" />
      <ExtensionItem Key="Key_0" Value="Value_1" />
    </ns3:Extensions>
    <Destination>Star</Destination>
  </ns2:Header>
</ns0:DTLSecurity>
"""

我使用了以下代码但收到错误 -

        def xml = new XmlParser(false, false).parseText(temp)
        //def xml = new XmlParser().parse(xmlFile)

        xml.Issue_Price.each{
            //trade = it
            it.value = "100"
           }
        xml.Issue_Currency_code.each{
            //status = it
            it.value = "USD"
            log.info it.value
            //log.info xml.Issue_Currency_code
             }
        def dir = "D:\\Invesco\\"
        def xFile1 = "InputFile-Trade-"+data[0]+"-Status1-"+data[1]+".xml"
        def xmlFile1 = dir+xFile1
        def printer = new XmlNodePrinter(new PrintWriter(xmlFile1))
        printer.preserveWhitespace = true
        printer.print(xml)

1 个答案:

答案 0 :(得分:0)

尝试:

,而不是每两个块
    xml.'**'.'ns1:Instrument_Listing'.each{
        //trade = it
        it.@Issue_Price = "100"
        it.@Currency_Code = "USD"
    }