我有一个输入XML,如下所示. 通过替换位于"SwapTermList"父标记下的&lt;trade_qty&gt;标记的CDATA内的值,我想从该XML形成一个新的XML字符串.例如<trade_qty><![CDATA[25.0]]></trade_qty>//需要用我要传递的另一个值替换25.0 类似地,也替换位于"TermPriceList"父标记下&lt;FIXED_PRICE&>标记CDATA内的值,而该标记也位于"SwapTermList"父标记下.

<fixed_price><![CDATA[92.053]]></fixed_price>//用我将传递的另一个值替换92.053

示例XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
  <SOAP-ENV:Header TransactionID="7578641ebc849609668e4b69e6b1f088c5ec0e2d0b6ae0" controllerName="CONTROLLER_1" hostIPAddress="10.21.75.203" serverEndTime="22:40:54.382" serverStartTime="22:40:54.366" serverTimeInMs="15"/>
  <SOAP-ENV:Body>
    <ns1:GetTradeResponse xmlns:ns1="urn:TradeData" IsCompress="FALSE" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <return xsi:type="xsd:string">
        <response>
          <mhead name="TPTResGetTrade">
            <sender>TriplePointClient</sender>
          </mhead>
          <mbody>
            <data>
              <GetTrade>
                <TradeHeader table="1">
                  <trade_status_ind><![CDATA[1]]></trade_status_ind>
                  <comm_uom_cd><![CDATA[MT]]></comm_uom_cd>
                  <participant_reloaded_ind><![CDATA[1]]></participant_reloaded_ind>
                  <deal_id><![CDATA[93174763]]></deal_id>
                  <internal_ind><![CDATA[0]]></internal_ind>
                  <mirror_doc_ind><![CDATA[0]]></mirror_doc_ind>
                  <permission_ind><![CDATA[1]]></permission_ind>
                  <trade_type_cd><![CDATA[Swap (L)]]></trade_type_cd>
                  <delivery_based_ind><![CDATA[0]]></delivery_based_ind>
                  <process_group_cd><![CDATA[MUM Physical]]></process_group_cd>
                  <xch_internal_comp_type_ind><![CDATA[-1]]></xch_internal_comp_type_ind>
                  <trader_person_num><![CDATA[151]]></trader_person_num>
                  <strategy_num><![CDATA[4142]]></strategy_num>
                  <language_cd><![CDATA[en]]></language_cd>
                  <comm_curr_cd><![CDATA[USD]]></comm_curr_cd>
                  <inter_company_ind><![CDATA[0]]></inter_company_ind>
                  <trade_linked_ind><![CDATA[0]]></trade_linked_ind>
                  <broker2_spread_ind><![CDATA[0]]></broker2_spread_ind>
                  <cleared_ind><![CDATA[-1]]></cleared_ind>
                  <broker_spread_ind><![CDATA[0]]></broker_spread_ind>
                  <trade_dt><![CDATA[20230913000000]]></trade_dt>
                  <commission_type_ind><![CDATA[0]]></commission_type_ind>
                  <notification_ind><![CDATA[0]]></notification_ind>
                  <internal_company_num><![CDATA[4392]]></internal_company_num>
                  <fx_spot_trade_ind><![CDATA[0]]></fx_spot_trade_ind>
                  <broker_company_num><![CDATA[4103]]></broker_company_num>
                  <instrument_type_cd><![CDATA[Financial Swap]]></instrument_type_cd>
                  <data_source_cd><![CDATA[XL]]></data_source_cd>
                  <confirm_approval_ind><![CDATA[0]]></confirm_approval_ind>
                  <is_delivery_upload><![CDATA[0]]></is_delivery_upload>
                  <counterpart_company_num><![CDATA[4175]]></counterpart_company_num>
                  <internal_side_ind><![CDATA[1]]></internal_side_ind>
                  <trade_num><![CDATA[902495]]></trade_num>
                  <ref1_enabled_ind><![CDATA[1]]></ref1_enabled_ind>
                  <hedge_ind><![CDATA[0]]></hedge_ind>
                  <whatif_trade_ind><![CDATA[0]]></whatif_trade_ind>
                  <xch_type_ind><![CDATA[0]]></xch_type_ind>
                  <commission_amt><![CDATA[0.05]]></commission_amt>
                  <ref2_enabled_ind><![CDATA[1]]></ref2_enabled_ind>
                  <inter_comp_ind><![CDATA[0]]></inter_comp_ind>
                  <deal_name_str><![CDATA[Swap (L) from]]></deal_name_str>
                  <SwapTermList table="2">
                    <buy_sell_ind><![CDATA[1]]></buy_sell_ind>
                    <reporting_period_offset><![CDATA[0]]></reporting_period_offset>
                    <settlement_curr_cd><![CDATA[USD]]></settlement_curr_cd>
                    <term_section_cd><![CDATA[902495-1]]></term_section_cd>
                    <use_default_instruction_ind><![CDATA[1]]></use_default_instruction_ind>
                    <mtm_factor><![CDATA[100.0]]></mtm_factor>
                    <delivery_start_dt><![CDATA[20231001000000]]></delivery_start_dt>
                    <customs_duty_status_ind><![CDATA[-1]]></customs_duty_status_ind>
                    <settle_payment_term_cd><![CDATA[14 CAL NYBnk]]></settle_payment_term_cd>
                    <alt_price_conv_ind><![CDATA[0]]></alt_price_conv_ind>
                    <cmdty_cd><![CDATA[Dubai]]></cmdty_cd>
                    <strategy_num><![CDATA[4142]]></strategy_num>
                    <last_modify_dt><![CDATA[20230913124059]]></last_modify_dt>
                    <to_uom_cd><![CDATA[MT]]></to_uom_cd>
                    <pricing_schedule_ind><![CDATA[1]]></pricing_schedule_ind>
                    <source_action_ind><![CDATA[0]]></source_action_ind>
                    <term_dt><![CDATA[20230913000000]]></term_dt>
                    <deliv_sched_cd><![CDATA[Monthly]]></deliv_sched_cd>
                    <trade_term_ind><![CDATA[1]]></trade_term_ind>
                    <allocated_ind><![CDATA[0]]></allocated_ind>
                    <scheduled_ind><![CDATA[0]]></scheduled_ind>
                    <location_num><![CDATA[2566]]></location_num>
                    <timezone_cd><![CDATA[GMT]]></timezone_cd>
                    <from_uom_cd><![CDATA[BBL]]></from_uom_cd>
                    <term_input_dt><![CDATA[20230913000000]]></term_input_dt>
                    <term_num><![CDATA[6709]]></term_num>
                    <settlement_uom_cd><![CDATA[BBL]]></settlement_uom_cd>
                    <delivery_end_dt><![CDATA[20231031000000]]></delivery_end_dt>
                    <trade_qty><![CDATA[**25.0**]]></trade_qty>
                    <instrument_type_cd><![CDATA[Financial Swap]]></instrument_type_cd>
                    <delivery_start_period><![CDATA[HE01]]></delivery_start_period>
                    <delivery_end_period><![CDATA[HE24]]></delivery_end_period>
                    <use_option_model_vol_ind><![CDATA[0]]></use_option_model_vol_ind>
                    <mtm_schedule_ind><![CDATA[1]]></mtm_schedule_ind>
                    <bonafide_hedge_ind><![CDATA[0]]></bonafide_hedge_ind>
                    <bonafide_hedge_type_ind><![CDATA[-1]]></bonafide_hedge_type_ind>
                    <lc_required_ind><![CDATA[0]]></lc_required_ind>
                    <end_dt><![CDATA[20231031000000]]></end_dt>
                    <bonafide_claimant_ind><![CDATA[-1]]></bonafide_claimant_ind>
                    <qty_periodicity_ind><![CDATA[2]]></qty_periodicity_ind>
                    <start_dt><![CDATA[20231001000000]]></start_dt>
                    <uom_conversion_rate><![CDATA[7.231608936927271]]></uom_conversion_rate>
                    <qty_uom_cd><![CDATA[zKBBL]]></qty_uom_cd>
                    <hdg_acct_import_status_ind><![CDATA[0]]></hdg_acct_import_status_ind>
                    <extended_qty><![CDATA[25.0]]></extended_qty>
                    <volume_computation_ind><![CDATA[0]]></volume_computation_ind>
                    <TermScrList table="2">
                      <settlement_curr_cd><![CDATA[USD]]></settlement_curr_cd>
                      <price_uom_period_ind><![CDATA[-1]]></price_uom_period_ind>
                      <common_pricing_days_ind><![CDATA[0]]></common_pricing_days_ind>
                      <price_qty_category_ind><![CDATA[0]]></price_qty_category_ind>
                      <percentage_cashflow_qty_ind><![CDATA[-1]]></percentage_cashflow_qty_ind>
                      <internal_ind><![CDATA[0]]></internal_ind>
                      <delivery_based_ind><![CDATA[0]]></delivery_based_ind>
                      <trader_person_num><![CDATA[151]]></trader_person_num>
                      <period_start_dt><![CDATA[20231001000000]]></period_start_dt>
                      <term_pricing_num><![CDATA[25770]]></term_pricing_num>
                      <strategy_num><![CDATA[4142]]></strategy_num>
                      <tier_type_ind><![CDATA[-1]]></tier_type_ind>
                      <implied_cost_ind><![CDATA[0]]></implied_cost_ind>
                      <book_upon_match_ind><![CDATA[0]]></book_upon_match_ind>
                      <do_not_apply_ra_cost_ind><![CDATA[0]]></do_not_apply_ra_cost_ind>
                      <rate_lookup_cost_ind><![CDATA[0]]></rate_lookup_cost_ind>
                      <round_precision_ind><![CDATA[0]]></round_precision_ind>
                      <cost_source_ind><![CDATA[-1]]></cost_source_ind>
                      <estimated_volume><![CDATA[100.0]]></estimated_volume>
                      <mix_defn_num><![CDATA[0]]></mix_defn_num>
                      <term_pricing_comp_num><![CDATA[27072]]></term_pricing_comp_num>
                      <effective_dt><![CDATA[20230913000000]]></effective_dt>
                      <multiplier><![CDATA[1.0]]></multiplier>
                      <mirror_scr_num><![CDATA[0]]></mirror_scr_num>
                      <period_end_dt><![CDATA[20231031000000]]></period_end_dt>
                      <pass_thru_ind><![CDATA[0]]></pass_thru_ind>
                      <actual_estimate_ind><![CDATA[0]]></actual_estimate_ind>
                      <tier_qty_periodicity_ind><![CDATA[-1]]></tier_qty_periodicity_ind>
                      <cumulative_ind><![CDATA[0]]></cumulative_ind>
                      <period_sub_category_ind><![CDATA[-1]]></period_sub_category_ind>
                      <componentize_status_ind><![CDATA[-1]]></componentize_status_ind>
                      <tier_boundary_ind><![CDATA[-1]]></tier_boundary_ind>
                      <hidden_cost_ind><![CDATA[0]]></hidden_cost_ind>
                      <round_pricing_component_ind><![CDATA[0]]></round_pricing_component_ind>
                      <manual_override_ind><![CDATA[0]]></manual_override_ind>
                      <pl_allocation_rule_ind><![CDATA[0]]></pl_allocation_rule_ind>
                      <apply_precision_ind><![CDATA[1]]></apply_precision_ind>
                      <scr_num><![CDATA[104122]]></scr_num>
                      <fixed_price><![CDATA[0.05]]></fixed_price>
                      <pricingComponentCount><![CDATA[0]]></pricingComponentCount>
                      <input_dt><![CDATA[20230913000000]]></input_dt>
                      <demand_charge_ind><![CDATA[0]]></demand_charge_ind>
                      <cost_revenue_ind><![CDATA[-1]]></cost_revenue_ind>
                      <payment_due_dt><![CDATA[20230913000000]]></payment_due_dt>
                      <scr_generated_ind><![CDATA[1]]></scr_generated_ind>
                      <pricing_type_ind><![CDATA[0]]></pricing_type_ind>
                      <estimate_audit_num><![CDATA[19328]]></estimate_audit_num>
                      <comp_function_ind><![CDATA[-1]]></comp_function_ind>
                      <use_pricing_dt_ind><![CDATA[0]]></use_pricing_dt_ind>
                      <company_num><![CDATA[4103]]></company_num>
                      <floor_book_upon_match_ind><![CDATA[0]]></floor_book_upon_match_ind>
                      <complex_charges_ind><![CDATA[0]]></complex_charges_ind>
                      <balance_cost_ind><![CDATA[0]]></balance_cost_ind>
                      <rate_override_ind><![CDATA[0]]></rate_override_ind>
                      <cost_type_cd><![CDATA[Broker Fee]]></cost_type_cd>
                      <fx_rate_type_ind><![CDATA[-1]]></fx_rate_type_ind>
                      <pricing_period_tier_ind><![CDATA[-1]]></pricing_period_tier_ind>
                      <tier_qty_type_ind><![CDATA[-1]]></tier_qty_type_ind>
                      <time_based_cost_ind><![CDATA[0]]></time_based_cost_ind>
                      <price_uom_cd><![CDATA[MT]]></price_uom_cd>
                      <internal_company_num><![CDATA[4392]]></internal_company_num>
                      <ic_mirror_cost_ind><![CDATA[0]]></ic_mirror_cost_ind>
                      <ic_scr_mirror_cost_ind><![CDATA[0]]></ic_scr_mirror_cost_ind>
                      <rate_lookup_cost_type_ind><![CDATA[0]]></rate_lookup_cost_type_ind>
                      <data_source_cd><![CDATA[XL]]></data_source_cd>
                      <price_curr_cd><![CDATA[USD]]></price_curr_cd>
                      <round_pricing_rule_ind><![CDATA[0]]></round_pricing_rule_ind>
                      <broker_type_ind><![CDATA[1]]></broker_type_ind>
                      <inter_comp_ind><![CDATA[0]]></inter_comp_ind>
                      <retain_realized_ind><![CDATA[0]]></retain_realized_ind>
                    </TermScrList>
                    <TermUdfList table="2">
                      <udf_value><![CDATA[2]]></udf_value>
                      <operation><![CDATA[-1]]></operation>
                      <udf_cd><![CDATA[Deal Time]]></udf_cd>
                    </TermUdfList>
                    <TermUdfList table="2">
                      <udf_value><![CDATA[odcril_cgmlTradesINT_rel_files_202309130251.csv]]></udf_value>
                      <operation><![CDATA[-1]]></operation>
                      <udf_cd><![CDATA[File Name1]]></udf_cd>
                    </TermUdfList>
                    <TermMtmList table="2">
                      <price_uom_period_ind><![CDATA[-1]]></price_uom_period_ind>
                      <common_pricing_days_ind><![CDATA[0]]></common_pricing_days_ind>
                      <price_qty_category_ind><![CDATA[-1]]></price_qty_category_ind>
                      <percentage_cashflow_qty_ind><![CDATA[-1]]></percentage_cashflow_qty_ind>
                      <delivery_based_ind><![CDATA[0]]></delivery_based_ind>
                      <period_start_dt><![CDATA[20231001000000]]></period_start_dt>
                      <term_pricing_num><![CDATA[25769]]></term_pricing_num>
                      <tier_type_ind><![CDATA[-1]]></tier_type_ind>
                      <implied_cost_ind><![CDATA[0]]></implied_cost_ind>
                      <do_not_apply_ra_cost_ind><![CDATA[0]]></do_not_apply_ra_cost_ind>
                      <rate_lookup_cost_ind><![CDATA[0]]></rate_lookup_cost_ind>
                      <round_precision_ind><![CDATA[0]]></round_precision_ind>
                      <cost_source_ind><![CDATA[-1]]></cost_source_ind>
                      <estimated_volume><![CDATA[100.0]]></estimated_volume>
                      <mix_defn_num><![CDATA[0]]></mix_defn_num>
                      <term_pricing_comp_num><![CDATA[27071]]></term_pricing_comp_num>
                      <event_specific_pricing_ind><![CDATA[0]]></event_specific_pricing_ind>
                      <multiplier><![CDATA[1.0]]></multiplier>
                      <price_precision><![CDATA[3]]></price_precision>
                      <period_end_dt><![CDATA[20231031000000]]></period_end_dt>
                      <actual_estimate_ind><![CDATA[0]]></actual_estimate_ind>
                      <tier_qty_periodicity_ind><![CDATA[-1]]></tier_qty_periodicity_ind>
                      <cumulative_ind><![CDATA[0]]></cumulative_ind>
                      <period_sub_category_ind><![CDATA[-1]]></period_sub_category_ind>
                      <componentize_status_ind><![CDATA[-1]]></componentize_status_ind>
                      <tier_boundary_ind><![CDATA[-1]]></tier_boundary_ind>
                      <hidden_cost_ind><![CDATA[0]]></hidden_cost_ind>
                      <round_pricing_component_ind><![CDATA[0]]></round_pricing_component_ind>
                      <manual_override_ind><![CDATA[0]]></manual_override_ind>
                      <apply_precision_ind><![CDATA[1]]></apply_precision_ind>
                      <pricingComponentCount><![CDATA[0]]></pricingComponentCount>
                      <demand_charge_ind><![CDATA[0]]></demand_charge_ind>
                      <pricing_type_ind><![CDATA[1]]></pricing_type_ind>
                      <estimate_audit_num><![CDATA[19327]]></estimate_audit_num>
                      <comp_function_ind><![CDATA[-1]]></comp_function_ind>
                      <use_pricing_dt_ind><![CDATA[0]]></use_pricing_dt_ind>
                      <complex_charges_ind><![CDATA[0]]></complex_charges_ind>
                      <balance_cost_ind><![CDATA[0]]></balance_cost_ind>
                      <rate_override_ind><![CDATA[0]]></rate_override_ind>
                      <cost_type_cd><![CDATA[MTM]]></cost_type_cd>
                      <fx_rate_type_ind><![CDATA[-1]]></fx_rate_type_ind>
                      <quote_def_num><![CDATA[10320]]></quote_def_num>
                      <pricing_period_tier_ind><![CDATA[-1]]></pricing_period_tier_ind>
                      <tier_qty_type_ind><![CDATA[-1]]></tier_qty_type_ind>
                      <time_based_cost_ind><![CDATA[0]]></time_based_cost_ind>
                      <ic_mirror_cost_ind><![CDATA[0]]></ic_mirror_cost_ind>
                      <rate_lookup_cost_type_ind><![CDATA[0]]></rate_lookup_cost_type_ind>
                      <round_pricing_rule_ind><![CDATA[0]]></round_pricing_rule_ind>
                      <retain_realized_ind><![CDATA[0]]></retain_realized_ind>
                    </TermMtmList>
                    <TermDetailList table="2">
                      <mon_ind><![CDATA[1]]></mon_ind>
                      <sat_ind><![CDATA[1]]></sat_ind>
                      <end_time_period_cd><![CDATA[HE24]]></end_time_period_cd>
                      <fri_ind><![CDATA[1]]></fri_ind>
                      <trade_status_ind><![CDATA[1]]></trade_status_ind>
                      <tue_ind><![CDATA[1]]></tue_ind>
                      <term_num><![CDATA[6709]]></term_num>
                      <sun_ind><![CDATA[1]]></sun_ind>
                      <var_project_delta><![CDATA[100.0]]></var_project_delta>
                      <trade_qty><![CDATA[25.0]]></trade_qty>
                      <thu_ind><![CDATA[1]]></thu_ind>
                      <comp_sync_id><![CDATA[1]]></comp_sync_id>
                      <start_time_period_cd><![CDATA[HE01]]></start_time_period_cd>
                      <detail_num><![CDATA[8520]]></detail_num>
                      <end_dt><![CDATA[20231031000000]]></end_dt>
                      <qty_periodicity_ind><![CDATA[2]]></qty_periodicity_ind>
                      <start_dt><![CDATA[20231001000000]]></start_dt>
                      <day_offset_ind><![CDATA[0]]></day_offset_ind>
                      <hol_ind><![CDATA[1]]></hol_ind>
                      <qty_uom_cd><![CDATA[zKBBL]]></qty_uom_cd>
                      <wed_ind><![CDATA[1]]></wed_ind>
                    </TermDetailList>
                    <TermPriceList table="2">
                      <price_uom_period_ind><![CDATA[-1]]></price_uom_period_ind>
                      <common_pricing_days_ind><![CDATA[0]]></common_pricing_days_ind>
                      <price_qty_category_ind><![CDATA[0]]></price_qty_category_ind>
                      <percentage_cashflow_qty_ind><![CDATA[-1]]></percentage_cashflow_qty_ind>
                      <delivery_based_ind><![CDATA[0]]></delivery_based_ind>
                      <period_start_dt><![CDATA[20231001000000]]></period_start_dt>
                      <term_pricing_num><![CDATA[25768]]></term_pricing_num>
                      <tier_type_ind><![CDATA[-1]]></tier_type_ind>
                      <implied_cost_ind><![CDATA[0]]></implied_cost_ind>
                      <do_not_apply_ra_cost_ind><![CDATA[0]]></do_not_apply_ra_cost_ind>
                      <rate_lookup_cost_ind><![CDATA[0]]></rate_lookup_cost_ind>
                      <round_precision_ind><![CDATA[0]]></round_precision_ind>
                      <cost_source_ind><![CDATA[-1]]></cost_source_ind>
                      <estimated_volume><![CDATA[100.0]]></estimated_volume>
                      <mix_defn_num><![CDATA[0]]></mix_defn_num>
                      <term_pricing_comp_num><![CDATA[27070]]></term_pricing_comp_num>
                      <event_specific_pricing_ind><![CDATA[0]]></event_specific_pricing_ind>
                      <multiplier><![CDATA[1.0]]></multiplier>
                      <price_precision><![CDATA[3]]></price_precision>
                      <period_end_dt><![CDATA[20231031000000]]></period_end_dt>
                      <actual_estimate_ind><![CDATA[0]]></actual_estimate_ind>
                      <tier_qty_periodicity_ind><![CDATA[-1]]></tier_qty_periodicity_ind>
                      <cumulative_ind><![CDATA[0]]></cumulative_ind>
                      <period_sub_category_ind><![CDATA[-1]]></period_sub_category_ind>
                      <componentize_status_ind><![CDATA[-1]]></componentize_status_ind>
                      <tier_boundary_ind><![CDATA[-1]]></tier_boundary_ind>
                      <hidden_cost_ind><![CDATA[0]]></hidden_cost_ind>
                      <round_pricing_component_ind><![CDATA[0]]></round_pricing_component_ind>
                      <manual_override_ind><![CDATA[0]]></manual_override_ind>
                      <apply_precision_ind><![CDATA[1]]></apply_precision_ind>
                      <fixed_price><![CDATA[**92.053**]]></fixed_price>
                      <pricingComponentCount><![CDATA[0]]></pricingComponentCount>
                      <demand_charge_ind><![CDATA[0]]></demand_charge_ind>
                      <pricing_type_ind><![CDATA[0]]></pricing_type_ind>
                      <estimate_audit_num><![CDATA[19326]]></estimate_audit_num>
                      <comp_function_ind><![CDATA[-1]]></comp_function_ind>
                      <use_pricing_dt_ind><![CDATA[0]]></use_pricing_dt_ind>
                      <complex_charges_ind><![CDATA[0]]></complex_charges_ind>
                      <balance_cost_ind><![CDATA[0]]></balance_cost_ind>
                      <rate_override_ind><![CDATA[0]]></rate_override_ind>
                      <cost_type_cd><![CDATA[Primary Settlement]]></cost_type_cd>
                      <fx_rate_type_ind><![CDATA[-1]]></fx_rate_type_ind>
                      <pricing_period_tier_ind><![CDATA[-1]]></pricing_period_tier_ind>
                      <tier_qty_type_ind><![CDATA[-1]]></tier_qty_type_ind>
                      <time_based_cost_ind><![CDATA[0]]></time_based_cost_ind>
                      <price_uom_cd><![CDATA[BBL]]></price_uom_cd>
                      <ic_mirror_cost_ind><![CDATA[0]]></ic_mirror_cost_ind>
                      <rate_lookup_cost_type_ind><![CDATA[0]]></rate_lookup_cost_type_ind>
                      <price_curr_cd><![CDATA[USD]]></price_curr_cd>
                      <round_pricing_rule_ind><![CDATA[0]]></round_pricing_rule_ind>
                      <retain_realized_ind><![CDATA[0]]></retain_realized_ind>
                    </TermPriceList>
                  </SwapTermList>
                </TradeHeader>
                <LockNumList table="1">
                  <lockinfo><![CDATA[TRD_HEADER 902495  1       1
    ACT_CASHFLOW    26599           1
    ACT_CASHFLOW    26600           1
    TRD_TERM    6709            1
    ]]></lockinfo>
                </LockNumList>
                <Permissions table="1">
                  <security><![CDATA[0]]></security>
                </Permissions>
              </GetTrade>
            </data>
          </mbody>
        </response>
      </return>
    </ns1:GetTradeResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我已经从我拥有的现有交易字符串创建了Document对象,并try 了一些代码来获取元素.但坦率地说,我不知道如何才能替换这些值,记住它们是在CDATA中替换的(尽管我不介意替换整个 node ,如trade_qty本身),而且也只在特定的父 node 下.

private Document parseXML(String xml) throws ParserConfigurationException, SAXException, IOException
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(xml)));
        doc.getDocumentElement().normalize();
        return doc;
    }

public void readXML(String tradeResponseXML)
    {
        String replacedXML = "";
        Document doc = null;
        try {
            doc = parseXML(tradeResponseXML);
        }
        catch (ParserConfigurationException | SAXException  | IOException xe)
        {
            logger.error("Error parsing the get response xml: ", xe);
        }

        if(doc != null)
        {
            NodeList nList = doc.getElementsByTagName(SWAP_TERM_LIST_XML_ELEM);
            for (int i = 0; i < nList.getLength(); i++)
            {
                Node nNode = nList.item(i);
                Element eElement = (Element) nNode;
                Element cElement =  (Element) eElement.getElementsByTagName(TRADE_QTY_XML_ELEM).item(0);
                logger.info("Qty : " + cElement.getTextContent());
            }
        }
    }

或以下点网代码的类似功能:

For all node name like Get Trade,
    if (rootnodes.Name == "GetTrade")
    {
        foreach (XmlNode t_node in rootnodes.ChildNodes)
        {
            if (t_node.Name == "TradeHeader")
            {
                foreach (XmlNode s_node in t_node.ChildNodes)
                {
                    if (s_node.Name == "SwapTermList")
                    {

                        foreach (XmlNode p_node in s_node.ChildNodes)
                        {
                            if (p_node.Name == "trade_qty")
                            {
                                ColName = p_node.Name;
                                ColValue = p_node.InnerText;

                                sResponse1 = sResponse1.Replace("<trade_qty><![CDATA[" + Math.Abs(Convert.ToDecimal(ColValue)).ToString() + "]]></trade_qty>", "<trade_qty><![CDATA[" + Math.Abs(Convert.ToDecimal(s.ItemArray.GetValue(4))).ToString() + "]]></trade_qty>");//(It's lots value.)
                            }

                            if (p_node.Name == "TermPriceList")
                            {
                                foreach (XmlNode k_node in p_node.ChildNodes)
                                {
                                    if (k_node.Name == "fixed_price")
                                    {
                                        ColName = k_node.Name;
                                        ColValue = k_node.InnerText;
                                        errorLog.log.LogFile("COlname to update price: " + ColName);
                                        errorLog.log.LogFile("COlValue to update price: " + ColValue);
                                        sResponse1 = sResponse1.Replace("<fixed_price><![CDATA[" + Math.Abs(Convert.ToDecimal(ColValue)).ToString() + "]]></fixed_price>", "<fixed_price><![CDATA[" + Math.Abs(Convert.ToDecimal(s.ItemArray.GetValue(10))).ToString() + "]]></fixed_price>");// its TradePrice value.
                                        errorLog.log.LogFile("Response for fixed price : " + sResponse1);
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }
    }

推荐答案

你可以试着用Jsoup.JSoup实际上是一个html解析器,但也能够解析XML.它非常直观,一旦你熟悉了selector syntax,它就非常容易使用.您可以将CDATA的内容解析为CDataNode,并使用内置方法来获取所需内容.

Maven依赖关系:

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.16.1</version>
</dependency>

使用JSoup您的代码可以简单地替换这些值,如下所示

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

import org.jsoup.Jsoup;
import org.jsoup.nodes.CDataNode;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;

public class Example {

    public static void main(String[] args) throws IOException {

        InputStream inputStream = new FileInputStream(new File("/path/to/your/test.xml"));
        Document document = Jsoup.parse(inputStream, "UTF-8", "", Parser.xmlParser());

        CDataNode trade_qty = (CDataNode) document.selectFirst("SwapTermList trade_qty").childNode(0);
        CDataNode fixed_price = (CDataNode) document.selectFirst("SwapTermList TermScrList fixed_price").childNode(0);

        System.out.println("original values");

        System.out.println(trade_qty.text());
        System.out.println(fixed_price.text());

        //replace cdata text
        trade_qty.text("5555");
        fixed_price.text("7777");

        System.out.println("replaced values");
        System.out.println(trade_qty.text());
        System.out.println(fixed_price.text());

        //if you want to save the modified document to a new file use below write, else delete the 2 lines below
        document.outputSettings().prettyPrint(false);
        Files.writeString(Path.of("/path/to/your/test_out.xml"), document.outerHtml());
    }
}

Update

如果我需要替换所有的trade_qty实例,而只需要替换最终价格的一个实例(父"TermPriceList"下的那个),该怎么办呢?

为了 Select 具有相同标签的多个元素,使用方法select而不是selectFirst.语法相似,不同之处在于它们的返回类型,而SELECT返回Elements,seltFirst返回Element.在您的示例XML中,我没有看到任何具有final_price的标记.但如果有的话,程序与fixed_price是一样的.

如果您想要修改某个元素,您可以为 Select 参数提供几种可能性.例如,第n个子元素、第一个元素、元素x后跟元素y,依此类推.熟悉一下这里的selector syntax号.

我已经复制了前面的示例代码,并根据您的问题在适当的位置修改了它.希望这是不言而喻的.

public static void main(String[] args) throws IOException {

    InputStream inputStream = new FileInputStream(new File("/path/to/your/test.xml"));
    Document document = Jsoup.parse(inputStream, "UTF-8", "", Parser.xmlParser());

    Elements trade_qty_elements = document.select("SwapTermList trade_qty");
    CDataNode fixed_price = (CDataNode) document.selectFirst("SwapTermList TermScrList fixed_price").childNode(0);

    System.out.println("original values of trade_qty_elements");
    trade_qty_elements.forEach(element -> System.out.println(((CDataNode) element.childNode(0)).text()));

    System.out.println("original value of fixed_price");
    System.out.println(fixed_price.text());

    //replace cdata text for example fixed_price with 777 and all trade_qty elements with 999
    fixed_price.text("777");
    trade_qty_elements.forEach(element -> ((CDataNode) element.childNode(0)).text("999"));

    System.out.println("replaced values of trade_qty_elements");
    trade_qty_elements.forEach(element -> System.out.println(((CDataNode) element.childNode(0)).text()));
    System.out.println("replaced value of fixed_price");
    System.out.println(fixed_price.text());

    //if you want to save the modified document to a new file use below write, else delete the 2 lines below
    document.outputSettings().prettyPrint(false);
    Files.writeString(Path.of("/path/to/your/test_out.xml"), document.outerHtml());
}

Java相关问答推荐

Java SSLocket查明客户端是否发送了证书

try Dockerize Maven应用程序,但发布版本21不支持"

使用ExecutorService时在ThreadFactory中触发自定义newThread函数

如何使用CSS为选定但未聚焦的表格行设置背景 colored颜色 ?

Spring Batch 5-不要让它在数据库中自动创建表

如何修复IndexOutOfBoundsException在ClerView适配器的onRowMoved函数?

GSON期间的Java类型擦除

为什么Spring Boot项目无法为基于MySQL的CRUD应用程序找到从JPARepository接口扩展的ProductRepository?

在Eclipse中调试未导出的JDK模块的Java包

Kotlin Val是否提供了与Java最终版相同的可见性保证?

如何在透视表中添加对计数列的筛选?

STREAMS减少部分结果的问题

在Oracle db中,当我们提供字符串而不是数字时,比较是如何工作的?

有没有办法知道在合并中执行了什么操作?

有没有办法在o(log(N))中以系统的方式将数组中的小块元素复制和移动到新增长的数组中的左侧?

当使用不同的参数类型调用时,为什么围绕Objects.equals的类型安全包装不会失败?

Java CDI:@Singleton@Startup@Inject无法实现接口

如何在JSP中从select中获取值并将其放入另一个select

如何使用我的RLE程序解决此问题

ANTLR 接受特殊字符,例如 .标识符或表达式中的(点)和 ,(逗号)