使用xmlstarlet更改xml属性

时间:2018-06-18 14:27:41

标签: xml bash xmlstarlet

以下是我的xml文件的内容

  int lastCmp = Double.compare(this.price, p.getPrice());

例如,我希望能够更改

<?xml version="1.0" encoding="UTF-8"?>
<data-sources>
    <data-source id="mariaDB-162b8e6bd5d-176d08c17fde61cb" provider="mysql" driver="mariaDB" name="MariaDB - app" save-password="true" show-system-objects="true" read-only="false">
        <connection host="172.23.0.4" port="3306" server="" database="portal" url="jdbc:mysql://172.23.0.4:3306/portal" user="portal" password="fakepassword" home="/usr" type="dev">
            <network-handler type="TUNNEL" id="ssh_tunnel" enabled="false" save-password="false">
                <property name="sshConnectTimeout" value="10000"/>
                <property name="port" value="22"/>
                <property name="implementation" value="jsch"/>
                <property name="authType" value="PASSWORD"/>
            </network-handler>
            <network-handler type="PROXY" id="socks_proxy" enabled="false" save-password="false">
                <property name="socks-port" value="1080"/>
            </network-handler>
            <network-handler type="CONFIG" id="mysql_ssl" enabled="false" save-password="false">
                <property name="ssl.public.key.retrieve" value="false"/>
                <property name="ssl.verify.server" value="true"/>
                <property name="ssl.require" value="false"/>
            </network-handler>
        </connection>
    </data-source>
    <filters/>
</data-sources>

<connection host="172.23.0.4" port="3306" server="" database="portal" url="jdbc:mysql://172.23.0.4:3306/portal" user="portal" password="fakepassword" home="/usr" type="dev">

所以我需要动态更改主机属性和url。 我试过了:

<connection host="172.28.0.8" port="3306" server="" database="portal" url="jdbc:mysql://172.28.0.8:3306/portal" user="portal" password="fakepassword" home="/usr" type="dev">

但它不起作用,文件不会改变。

1 个答案:

答案 0 :(得分:1)

除了--inplace建议之外,还必须使用多个调用来更改多个属性

declare -A host=([old]=172.23.0.4 [new]=172.23.0.8) 
new_url="jdbc:mysql://${host[new]}:3306/portal"

xmlstarlet ed -u "//connection[@host = '${host[old]}']/@host" -v "${host[new]}" file.xml |
xmlstarlet ed -u "//connection[@host = '${host[new]}']/@url"  -v "$new_url"
<?xml version="1.0" encoding="UTF-8"?>
<data-sources>
  <data-source id="mariaDB-162b8e6bd5d-176d08c17fde61cb" provider="mysql" driver="mariaDB" name="MariaDB - app" save-password="true" show-system-objects="true" read-only="false">
    <connection host="172.23.0.8" port="3306" server="" database="portal" url="jdbc:mysql://172.28.0.8:3306/portal" user="portal" password="fakepassword" home="/usr" type="dev">
      <network-handler type="TUNNEL" id="ssh_tunnel" enabled="false" save-password="false">
        <property name="sshConnectTimeout" value="10000"/>
        <property name="port" value="22"/>
        <property name="implementation" value="jsch"/>
        <property name="authType" value="PASSWORD"/>
      </network-handler>
      <network-handler type="PROXY" id="socks_proxy" enabled="false" save-password="false">
        <property name="socks-port" value="1080"/>
      </network-handler>
      <network-handler type="CONFIG" id="mysql_ssl" enabled="false" save-password="false">
        <property name="ssl.public.key.retrieve" value="false"/>
        <property name="ssl.verify.server" value="true"/>
        <property name="ssl.require" value="false"/>
      </network-handler>
    </connection>
  </data-source>
  <filters/>
</data-sources>