gpt4 book ai didi

python - 使用 shell 或 Python 添加 XML 元素

转载 作者:行者123 更新时间:2023-12-01 01:15:41 29 4
gpt4 key购买 nike

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

我必须再插入一个元素

<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---- 下面文件中提到的位置

基本上在每个 标记结束之前

  <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>

请帮忙

最佳答案

在 shell 中使用 XMLStarlet 执行此操作将如下所示:

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 定义,因为问题的原始版本不包含任何定义)。

关于python - 使用 shell 或 Python 添加 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54374560/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com