尝试使用 xmlstarlet 向现有 xml 文件添加元素

Trying to add an element to existing xml file with xmlstarlet

我一直在尝试让 xmlstarlet 将 <security-enabled>false</security-enabled> 添加到“/configuration/core”。命令 运行s 没有错误,但现在对文件进行了更改。

XML 文件:

<configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
  <jms xmlns="urn:activemq:jms">
    <queue name="DLQ"/>
    <queue name="ExpiryQueue"/>
  </jms>
  <core xmlns="urn:activemq:core">
    <!-- this could be ASYNCIO or NIO
       -->
    <journal-type>ASYNCIO</journal-type>
    <paging-directory>./data/paging</paging-directory>
    <bindings-directory>./data/bindings</bindings-directory>
    <journal-directory>./data/journal</journal-directory>
    <large-messages-directory>./data/large-messages</large-messages-directory>
    <journal-min-files>10</journal-min-files>
    ...
  </core>
</configuration>

我有一个命令运行:

xmlstarlet ed -i "/configuration/core" -t attr -n "security-enabled" -v "false"  broker.xml

我也曾使用 xmlstarlet ed -L ... 进行原地编辑,但是当我注意到编辑没有发生时,我为 STDOUT 删除了 -L。

你需要specify xml namespace properly。在你的情况下:

xmlstarlet ed -L -N a="urn:activemq" -N c="urn:activemq:core" -s "/a:configuration/c:core" -t elem -n "security-enabled" -v "false" broker.xml

而且我改变了:

  • -t attr-t elem 因为我们需要元素而不是属性
  • -i-s 因为我们要求添加 sub 元素