0
votes

I am trying to replace password value with in a xml file with a new value stored in MyFile, can someone please let me know the ways through achieve this.Thanks

MyFile=test123

<Resource accessToUnderlyingConnectionAllowed="true" auth="Container" driverClassName="com.ibm.db2.jcc.DB2Driver" factory="com..tomcat.jndi.JSCommonsBasicDataSourceFactory" maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/server" password="myPassword"/>

Below error shown after executing the below command, please advise.

 xmlstarlet ed --omit-decl --inplace \ --update '//Resource/@password' --value "testingfile" \ /apps/META-INF/context.xml

failed to load external entity " --update"

 xmlstarlet ed --omit-decl --inplace --update '//Resource/@password' --value 'testingfile' /apps/META-INF/context.xml

/apps/META-INF/context.xml:26.119: AttValue: " or ' expected maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/server" password= ^ /apps/META-INF/context.xml:26.119: attributes construct error maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/server" password= ^ /apps/META-INF/context.xml:26.119: Couldn't find end of Start Tag Resource line 26 maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/server" password= ^

1
1. valid xml cannot have a space after the opening <; 2. you can't use curly quotes, stick to plain double quotes.glenn jackman
Don't Parse XML/HTML With Regex. I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...).Cyrus

1 Answers

3
votes

First, fix your XML

$ cat file.xml
<resources password="password"/>

Next, don't edit XML with anything except a real XML parser. is a handy one.

$ cat MyFile
test123
$ xmlstarlet ed --omit-decl --inplace \
                --update '//resources/@password' --value "$(cat MyFile)" \
                file.xml
$ cat file.xml
<resources password="test123"/>

To change the password for a particular resource, such as one with a specific value of the "name" attribute:

... --update '//resources[@name = "jdbc/server"]/@password' ...