Home > database >  Changing passwords in XML file with BASH or XMLStarlet
Changing passwords in XML file with BASH or XMLStarlet

Time:12-23

I have a bunch of XML that is used by a Java application. There are multiple "addService" blocks in the XML document but I am only interested updating the services used by MySQL connection.

Below is a sample block of XML data that require the password to be changed. How can I change the password value in in that block using BASH or XMLStarlet?

  <Call name="addService">
<Arg>
  <New >
    <Set name="Name">DataSourceService</Set>
    <Call name="addDataSource">
      <Arg>jdbc/MySQL</Arg>
      <Arg>
        <New >
          <Arg>
            <New >
              <Set name="dataSourceClassName">com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</Set>
              <Set name="maximumPoolSize">15</Set>
              <Set name="leakDetectionThreshold">10000</Set>
              <Call name="addDataSourceProperty">
                <Arg>url</Arg>
                <Arg>jdbc:mysql://localhost:3306/db?zeroDateTimeBehavior=convertToNull&amp;useSSL=false</Arg>
              </Call>
              <Call name="addDataSourceProperty">
                <Arg>user</Arg>
                <Arg>test</Arg>
              </Call>
              <Call name="addDataSourceProperty">
                <Arg>password</Arg>
                <Arg>some_password</Arg>
              </Call>
            </New>
          </Arg>
        </New>
      </Arg>
    </Call>
  </New>
</Arg>

Here's another XML block that requires password changes:

    <Call name="addService">
            <Arg>
                    <New >
                            <Set name="Name">DataSourceService</Set>
                            <Call name="addDataSource">
                                    <Arg>jdbc/MySQL</Arg>
                                    <Arg>
                                            <New >
                                                    <Set name="driverClass">com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</Set>
                                                    <Set name="jdbcUrl">jdbc:mysql://localhost:3306/db?zeroDateTimeBehavior=convertToNull</Set>
                                                    <Set name="user">test</Set>
                                                    <Set name="password">some_password</Set>
                                                    <Set name="acquireIncrement">5</Set>
                                                    <Set name="minConnectionsPerPartition">5</Set>
                                                    <Set name="maxConnectionsPerPartition">50</Set>
                                                    <Set name="idleConnectionTestPeriod">30</Set>
                                            </New>
                                    </Arg>
                            </Call>
                    </New>
            </Arg>
    </Call>

CodePudding user response:

Like this:

xmlstarlet ed -u '//Arg[text()="jdbc/MySQL"]//Call[@name="addDataSourceProperty"][3]/Arg[text()="some_password"]' -v new_password file
  • Related