ANT script to update <value></value> tag in xml file using properties file

975 views Asked by At

Please help me here with ant script to perform below mentiones task as i am stuck with it and not getting through.

I have properties files as below:

AccessSession/OperatorCode=Production

AccessSession/Password=%587931#

And so on....

And XML content is as below:( shortened xml content)

<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" 
name="PurchaseAdhocBundle">
<description></description>
<contact></contact>
<NVPairs name="Global Variables">
    <NameValuePair>
        <name>AccessSession/OperatorCode</name>
        <value>TM Production</value>
    </NameValuePair>
    <NameValuePair>
        <name>AccessSession/Password</name>
        <value>%T3lkom9525#</value>
    </NameValuePair>

And so on....

I want script to update value tag in XML file with actual property value given in properties file

<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" 
name="PurchaseAdhocBundle">
<description></description>
<contact></contact>
<NVPairs name="Global Variables">
    <NameValuePair>
        <name>AccessSession/OperatorCode</name>
        <value>Production</value>
    </NameValuePair>
    <NameValuePair>
        <name>AccessSession/Password</name>
        <value>%587931#</value>
    </NameValuePair>
1

There are 1 answers

3
Mark O'Connor On

I suggest using a filterset in the ANT copy task to substitute values into a template file.

Example

├── build.properties
├── build.xml
├── src
│   └── template.xml
└── target
    └── output.xml

build.properties

AccessSession/OperatorCode=Production
AccessSession/Password=%587931#

build.xml

<project name="demo" default="build">

  <property file="build.properties"/>

  <target name="build">
    <copy file="src/template.xml" tofile="target/output.xml">
      <filterset>
        <filter token="OPERATOR_CODE" value="${AccessSession/OperatorCode}"/>
        <filter token="PASSWORD" value="${AccessSession/Password}"/>
      </filterset>
    </copy>
  </target>

</project>

src/template.xml

Template that contains the replaceable tokens

<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" 
name="PurchaseAdhocBundle">
<description></description>
<contact></contact>
<NVPairs name="Global Variables">
    <NameValuePair>
        <name>AccessSession/OperatorCode</name>
        <value>@OPERATOR_CODE@</value>
    </NameValuePair>
    <NameValuePair>
        <name>AccessSession/Password</name>
        <value>@PASSWORD@</value>
    </NameValuePair>

target/output.xml

Output file generated by the ANT build.

<application xmlns="http://www.tibco.com/xmlns/ApplicationManagement" 
name="PurchaseAdhocBundle">
<description></description>
<contact></contact>
<NVPairs name="Global Variables">
    <NameValuePair>
        <name>AccessSession/OperatorCode</name>
        <value>Production</value>
    </NameValuePair>
    <NameValuePair>
        <name>AccessSession/Password</name>
        <value>%587931#</value>
    </NameValuePair>