本文介绍了使用Shell或Python添加XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要在多个位置插入XML元素

we have a requirement to insert XML element in multiple locations

我必须再插入一个元素

<wtc-import>
      <name>WTCImportedService-0-rap01</name>
      <resource-name>XXXX01F</resource-name>
      <local-access-point>lap01</local-access-point>
      <remote-access-point-list>rap01</remote-access-point-list>
      <remote-name>XXXX01F</remote-name>
    </wtc-import>

--- INSERT WTC IMPORT SERVICE ----在以下文件中提到的位置

in the ---INSERT WTC IMPORT SERVICE---- mentioned location in the below file

基本上在每个</wtc-server>标记

basically before the end of each < /wtc-server> tag

  <wtc-server>
    <name>WTC-server-117-02</name>
    <target>cwea_117_02</target>
    <wtc-local-tux-dom>
      <name>lap01</name>
      <access-point>lap01</access-point>
      <access-point-id>wl.prodcwe117.02</access-point-id>
      <block-time>600</block-time>
      <nw-addr>//localhost:7125</nw-addr>
    </wtc-local-tux-dom>
    <wtc-remote-tux-dom>
      <name>rap01</name>
      <access-point>rap01</access-point>
      <access-point-id>tx.hello101</access-point-id>
      <local-access-point>lap01</local-access-point>
      <nw-addr>//hostanme:71105</nw-addr>
      <federation-url></federation-url>
      <federation-name></federation-name>
    </wtc-remote-tux-dom>
    <wtc-import>
      <name>WTCImportedService-0-rap01</name>
      <resource-name>XXXX01F</resource-name>
      <local-access-point>lap01</local-access-point>
      <remote-access-point-list>rap01</remote-access-point-list>
      <remote-name>XXXX01F</remote-name>
    </wtc-import>

-------在此处插入新的WTC导入---------

  </wtc-server>

  <wtc-server>
    <name>WTC-server-117-03</name>
    <target>cwea_117_03</target>
    <wtc-local-tux-dom>
      <name>lap01</name>
      <access-point>lap01</access-point>
      <access-point-id>wl.prodcwe117.02</access-point-id>
      <block-time>600</block-time>
      <nw-addr>//localhost:7125</nw-addr>
    </wtc-local-tux-dom>
    <wtc-remote-tux-dom>
      <name>rap01</name>
      <access-point>rap01</access-point>
      <access-point-id>tx.hello101</access-point-id>
      <local-access-point>lap01</local-access-point>
      <nw-addr>//hostanme:71105</nw-addr>
      <federation-url></federation-url>
      <federation-name></federation-name>
    </wtc-remote-tux-dom>
    <wtc-import>
      <name>WTCImportedService-0-rap01</name>
      <resource-name>XXXX01F</resource-name>
      <local-access-point>lap01</local-access-point>
      <remote-access-point-list>rap01</remote-access-point-list>
      <remote-name>XXXX01F</remote-name>
    </wtc-import>

-------在此处插入新的WTC导入---------

 </wtc-server>

请帮助

推荐答案

使用XMLStarlet在shell中执行以下操作:

Doing this in shell with XMLStarlet would look like:

xmlstarlet ed \
  --subnode '//wtc-server' -t elem -n wtc-import \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n name -v WTCImportedService-0-rap01 \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n resource-name -v XXXX01F \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n local-access-point -v lap01 \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n remote-access-point-list -v rap01 \
  --subnode '//wtc-server/wtc-import[last()]' -t elem -n remote-name -v XXXX01F \
  <in.xml >out.xml

(测试的代码没有任何xmlns定义,因为问题的原始版本不包含任何定义).

(code tested without any xmlns definitions, since the original version of the question didn't contain any).

这篇关于使用Shell或Python添加XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:08