2

Here is the content of my xml file

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

For example I want to be able to change

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

to

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

So i need to change the host property and the url dynamically. I tried:

xmlstarlet ed -u "/data-sources/data-source/connection[@host]/@host" -v '172.28.0.8' .dbeaver-data-sources.xml

but it doesn't work, the file doesn't change.

1
  • 1
    What version of xmlstarlet? If you're using version 1.0.2 or higher, you can use the -L (or --inplace) command line argument to edit the file in place. Commented Jun 18, 2018 at 15:52

1 Answer 1

3

In addition to the --inplace advice, changing multiple attributes must be done with multiple invocations

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.