How to change values in xml file?

I have xml file like below, i want change the values at default-value place of each argument name using shell script. like
where argument name= protocol and default-value=tcp,
where argument name =port and default-value= 7223,
where argument name = username and default-value=test,

example

<task-arguments>
      <argument name="protocol" is-required="false" default-value="ssl"/>
      <argument name="port" is-required="true" default-value="7222"/>
      <argument name="username" is-required="true" default-value="admin"/>
</task-arguments>

could some one please help me..

What operating system and shell are you using?

Do you just want to change <argument name> tag data if the tag appears between <task-arguments> and </task-arguments> tags; or do you want to change them everywhere they appear?

Where do the new values come from? Is there a configuration file? Are they given as command-line arguments? Are they to be hardcoded in your script?

Are only the values shown in the example to be replaced, or are all values found in your input file to be replaced?

What is the name of your XML file? Is the name to be given as a parameter to your script? Is the name to be hard coded into your script?

What have you tried to solve this problem on your own?

Hi Don,
Please see my comment below..
What operating system and shell are you using?
RHEL

Do you just want to change <argument name> tag data if the tag appears between <task-arguments> and </task-arguments> tags; or do you want to change them everywhere they appear?

I want to change the values only the tag b/w <task-arguments> and </task-arguments> tags.

Where do the new values come from? Is there a configuration file? Are they given as command-line arguments? Are they to be hardcoded in your script?

Those values are coming from configuration file..

Are only the values shown in the example to be replaced, or are all values found in your input file to be replaced?

All the values to be replaced which are b/w the tag.

What is the name of your XML file? Is the name to be given as a parameter to your script? Is the name to be hard coded into your script?
monitor.xml and the value is hard coded

What have you tried to solve this problem on your own?
tried like below

sed -i '/<argument\s\+name="hostname"/s/is-required="*.*"/is-required="$REQ1" default-value="$hostname"/' $file

My crystal ball is cloudy today and I am having trouble reading over your shoulder...

What is the name of your configuration file?

What is the format of your configuration file?

Hi Don,,

I Have a script file like abc.sh when i run this script i want to update monitor.xml file like below..

The original file

<task-arguments>
            <argument name="hostname" is-required="true" default-value="10.211.55.4"/>
            <argument name="protocol" is-required="false" default-value="ssl"/>
            <argument name="port" is-required="true" default-value="7222"/>
            <argument name="username" is-required="true" default-value="admin"/>
            <argument name="password" is-required="true" default-value=""/>
            <argument name="tier" is-required="false" default-value="TIBCO_EMS"/>
            <argument name="emsservername" is-required="false" default-value="Tibco EMS Destinations"/>
            <argument name="showTempQueues" is-required="false" default-value="false"/>
            <argument name="showSysQueues" is-required="false" default-value="false"/>
            <argument name="queuesToExclude" is-required="false" default-value="sample1 sample2"/>
            <argument name="sslIdentityFile" is-required="false" default-value="test/conf/client_identity.p12"/>
            <argument name="sslIdentityPassword" is-required="false" default-value="password"/>
            <argument name="sslTrustedCerts" is-required="false" default-value="test/conf/server_cert.pem"/>
</task-arguments>

The file should update like below.

<task-arguments>
            <argument name="hostname" is-required="true" default-value="My hostname"/>
            <argument name="protocol" is-required="true" default-value="ssl"/>
            <argument name="port" is-required="true" default-value="7233"/>
            <argument name="username" is-required="true" default-value="admin"/>
            <argument name="password" is-required="true" default-value="admin"/>
            <argument name="tier" is-required="true" default-value="TIBCO_EMS"/>
            <argument name="emsservername" is-required="true" default-value="Tibco EMS Destinations"/>
            <argument name="showTempQueues" is-required="false" default-value="false"/>
            <argument name="showSysQueues" is-required="false" default-value="false"/>
            <argument name="queuesToExclude" is-required="true" default-value="sample1 sample2"/>
            <argument name="sslIdentityFile" is-required="false" default-value="test/conf/client_identity.p12"/>
            <argument name="sslIdentityPassword" is-required="false" default-value="password"/>
            <argument name="sslTrustedCerts" is-required="false" default-value="test/conf/server_cert.pem"/>
</task-arguments>

That means need find each argument name and should update the values accordingly... all values hard coded at script level..

And, what does abc.sh look like now? Please show us the contents of abc.sh (in CODE tags).

And, I ask again, what shell are you using?

Don,

The values means, Where ever it's highlighted in that place the values should update using my script file.

<task-arguments>
<argument name="hostname" is-required="true" default-value="My hostname"/>
<argument name="protocol" is-required="true" default-value="ssl"/>
<argument name="port" is-required="true" default-value="7233"/>
<argument name="username" is-required="true" default-value="admin"/>
<argument name="password" is-required="true" default-value="admin"/>
<argument name="tier" is-required="true" default-value="TIBCO_EMS"/>
<argument name="emsservername" is-required="true" default-value="Tibco EMS Destinations"/>
<argument name="showTempQueues" is-required="false" default-value="false"/>
<argument name="showSysQueues" is-required="false" default-value="false"/>
<argument name="queuesToExclude" is-required="true" default-value="sample1 sample2"/>
<argument name="sslIdentityFile" is-required="false" default-value="test/conf/client_identity.p12"/>
<argument name="sslIdentityPassword" is-required="false" default-value="password"/>
<argument name="sslTrustedCerts" is-required="false" default-value="test/conf/server_cert.pem"/>
</task-arguments>

---------- Post updated at 01:35 PM ---------- Previous update was at 01:24 PM ----------

Do,

I need to create new one script file and i am using my shell is !/bin/sh...

#!/bin/sh
#################################################
set -x
################################################################################
# Script Wide Variables                                                        #
################################################################################
dt=`date`
year=`echo ${dt} | cut -f6 -d ' '`
month=`date +%m`
day=`date +%d`
hr=`date +%H`
min=`date +%M`
diskVolume=/opt
diskSize=100MB
inputs=$*
################################################################################

In this script i am declaring the values like below.

hostname= 134.com
port=7223
protocal=tcp
username=admin
password=admin..etc..

I hope you understand everything now....

---------- Post updated at 01:40 PM ---------- Previous update was at 01:35 PM ----------

Don,,

I tried using sed command, it working but i am tying is there any another way to update values or not?

sed -i '/<argument\s\+name="hostname"/s/is-required="*.*"/is-required="${REQ1}" default-value="${hostname}"/' $file

I hope you understand my requirement here...

I find it difficult to believe that above sed script worked the desired way. Within single quotes, shell variables are not expanded, so the literal values like ${REQ1} would be inserted into the output:

      <argument name="hostname" is-required="${REQ1}" default-value="${hostname}"/>

And, the REQ1 variable is not defined anywhere, on top the hostname is set to the empty string when used as you posted.

As you again don't post your abc.sh script, it's impossible to propose improvements nor optimisation potential.

BTW, wouldn't it be easier to create the xml- file from scratch with the data from your config file?

Hi Rudi,

I gave only example in earlier post, not full of my script. I ma trying understand the logic to implement, I Will declare the values in script,how to pars the values my script will take care.

At the end i am looking a command to update the values at XML file...

---------- Post updated at 06:02 PM ---------- Previous update was at 04:03 PM ----------

Hi Rudi,

You are correct, my script is inserting ${REQ1} values only.. help me the right command to use.

You're not too helpful as you don't answer the questions raised.

Hi Rudi,

Yes, we can create xml file from scratch using data with my config file.

It's not possible to create the from scratch. I just need to update values in b/w the tags..

Please ask me the questions clearly. so that i can answer...

Hi Rudi,

I have used XMLSTARTLET package and used the bellow command, still no luck,

xml ed -u '/task-arguments/argument[@name="protocol"]/@default-value' -v 'string("tcp")' /tmp/test_xml/monitor.xml
xml ed -u '/task-arguments/argument[@name="port"]/@default-value' -v 'string("7223")' /tmp/test_xml/monitor.xml
xml ed -u '/task-arguments/argument[@name="username"]/@default-value' -v 'string("test")' /tmp/test_xml/monitor.xml

i am getting bellow error,

./sed.sh: line 4: 23932 Segmentation fault      xml ed -u '/task-arguments/argument[@name="protocol"]/@default-value' -v 'string("tcp")' /tmp/test_xml/monitor.xml
./sed.sh: line 5: 23933 Segmentation fault      xml ed -u '/task-arguments/argument[@name="port"]/@default-value' -v 'string("7223")' /tmp/test_xml/monitor.xml
./sed.sh: line 6: 23934 Segmentation fault      xml ed -u '/task-arguments/argument[@name="username"]/@default-value' -v 'string("test")' /tmp/test_xml/monitor.xml

please help me on this..

---------- Post updated at 06:07 PM ---------- Previous update was at 02:20 PM ----------

Hi Rudi,

The below one help me achieve my requirement..Thanks for your time

#!/bin/sh

xmlfile_in="data.xml"
xmlfile_out="data.new"

protocol="tcp"
port="7223"
user="test"

sed -f /dev/stdin "$xmlfile_in" >"$xmlfile_out" <<END_SED
/argument name="protocol"/{
    i\\
<argument name="protocol" is-required="true" default-value="$protocol"/>
    d
}

/argument name="port"/{
    i\\
<argument name="port" is-required="true" default-value="$port"/>
    d
}

/argument name="username"/{
    i\\
<argument name="username" is-required="true" default-value="$user"/>
    d
}
END_SED

You constantly ignore the requests to use code tags. Do you really think that helps?