加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Bash将子节点插入XML文件

发布时间:2020-12-15 18:58:37 所属栏目:安全 来源:网络整理
导读:我试图使用bash编辑配置文件.我的文件看起来像这样: configuration property name/name value/value /property property name/name value/value /property/configuration 我想添加另外两个 property阻止文件.由于所有属性标记都包含在配置标记内,因此文件将
我试图使用bash编辑配置文件.我的文件看起来像这样:
<configuration>
  <property>
    <name></name>
    <value></value>
  </property>
  <property>
    <name></name>
    <value></value>
  </property>
</configuration>

我想添加另外两个< property>阻止文件.由于所有属性标记都包含在配置标记内,因此文件将如下所示:

<configuration>
  <property>
    <name></name>
    <value></value>
  </property>
  <property>
    <name></name>
    <value></value>
  </property>
  <property>
    <name></name>
    <value></value>
  </property>
</configuration>

我遇到了this post and followed the accepted answer,但是我的文件没有附加任何内容,我尝试追加的xml块是“echo-ed”作为单行字符串.
我的bash文件如下所示:

file=/path/to/file/oozie-site.xml
content="<property>n<name></name>n<value></value>n</property>n<property>n<name></name>n<value></value>n</property>"
echo $content
C=$(echo $content | sed 's/////g')
sed "/</configuration>/ s/.*/${C}n&/" $file
使用xmlstarlet:
xmlstarlet ed --omit-decl 
  -s '//configuration' -t elem -n "property" 
  -s '//configuration/property[last()]' -t elem -n "name" 
  -s '//configuration/property[last()]' -t elem -n "value" 
  file.xml

输出:

<configuration>
  <property>
    <name/>
    <value/>
  </property>
  <property>
    <name/>
    <value/>
  </property>
  <property>
    <name/>
    <value/>
  </property>
</configuration>

--omit-decl: omit XML declaration

-s: add a subnode (see xmlstarlet ed for details)

-t elem: set node type,here: element

-n: set name of element

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读