0

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= ^

Kumar
  • 51
  • 1
  • 2
  • 10
  • 1. valid xml cannot have a space after the opening `<`; 2. you can't use curly quotes, stick to plain double quotes. – glenn jackman Oct 13 '20 at 22:34
  • 2
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Oct 13 '20 at 22:50

1 Answers1

3

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' ...
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Unable to execute, bash: xmlstarlet: command not found – Kumar Oct 13 '20 at 22:46
  • 1
    @Kumar Install it, then. – Shawn Oct 13 '20 at 23:57
  • 1
    It's as eaasy as `sudo apt install xmlstarlet` or `brew install xmlstarlet` or ... (adjust for your OS) – glenn jackman Oct 14 '20 at 03:37
  • after executing the above command I see failed to load external entity " --update", please see updated executed command.Thanks. – Kumar Oct 15 '20 at 03:12
  • The backslashes are for line continuations. If you're going to put the command all on line line, remove those backslashes. What you have with `\ --update` is the same as `" --update"` and the literal leading space prevents xmlstarlet from seeing the update option. – glenn jackman Oct 15 '20 at 03:26
  • 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= – Kumar Oct 15 '20 at 04:17
  • /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 – Kumar Oct 15 '20 at 04:18
  • Thank you Glenn, its working now, one quick question how do I change value only in one place in xml file where in my xml file we have more than 3 Resource name available and each resource tag has the password="", how do I search and apply the value only on that particular resource? – Kumar Oct 15 '20 at 04:40
  • That's an XPath question: `--update '//Resource[@name = "jdbc/test1"]/@password' --value "$newPassword"` – glenn jackman Oct 15 '20 at 13:34
  • I have found this page useful: https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256086(v=vs.100)?redirectedfrom=MSDN – glenn jackman Oct 15 '20 at 13:35