Search & Replace in xml file through dos comm

June 3, 2009 at 00:10:32
Specs: Windows XP
I want to replace some fo the xml tag in new xml file from old file.
e.g. i have old file with following
<add key="Root" value="346"/>
<add key=.....>
<add key=>
<add key="dbName" value="livedb"/>
<add key="ApplicationName" value="live"/>

i have new file (template)
<add key="Root" value="???"/>
<add key=.....>
<add key=>
<add key="dbName" value="???"/>
<add key="ApplicationName" value="???"/>

Now i want to replace value of 3 parameter/tag in new file from old one. i.e. replace value of Root, dbName and Application name.

How do i achive this through Dos batch file?


See More: Search & Replace in xml file through dos comm

Report •


#1
June 3, 2009 at 01:34:36
if you have Python on Windows
template="""
<add key="Root" value="%s"/>
<add key=.....>
<add key=>
<add key="dbName" value="%s"/>
<add key="ApplicationName" value="%ss"/>
"""
for line in open("oldfile.txt"):
    line=line.strip()
    if 'add key="Root"' in line:
        value=line.split("=")[-1].strip('"/>')
        rootvalue=value                 
    if 'add key="dbName"' in line:
        value=line.split("=")[-1].strip('"/>')
        dbnamevalue=value                         
    if 'add key="ApplicationName"' in line:
        value=line.split("=")[-1].strip('"/>')
        appnamevalue=value                         
print template %(rootvalue,dbnamevalue,appnamevalue)        
open("newfile.txt","w").write(template %(rootvalue,dbnamevalue,appnamevalue)+"\n")

usage:

C:\test>python test.py

<add key="Root" value="346"/>
<add key=.....>
<add key=>
<add key="dbName" value="livedb"/>
<add key="ApplicationName" value="lives"/>


C:\test>more newfile.txt

<add key="Root" value="346"/>
<add key=.....>
<add key=>
<add key="dbName" value="livedb"/>
<add key="ApplicationName" value="lives"/>


Report •

#2
June 4, 2009 at 02:24:12
Thanks for the reply.

I don't have python installed on my machine and no programming knowledge about it.

So good if someone can explain/give solution for Dos command.


Report •

#3
June 5, 2009 at 00:19:14
Maybe this will help.
Similar solution I found somewhere on this site.
====================================
@echo off > new.xml & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (my.xml) do (
set str=%%a
set str=!str:346=???!
set str=!str:livedb=???!
set str=!str:live=???!
echo !str! >> new.xml
)
====================================


Report •

Related Solutions

#4
June 5, 2009 at 02:00:43
@OP, here's an executable, tested on WinXP.
usage:
c:\test> test.exe oldfile.txt template.txt newfile.txt

so now you can use it without python installed.

Report •

#5
June 5, 2009 at 03:57:28
Thanks for the exe but its giving me following error.
Traceback (most recent call last):
File "<string>", line 16, in <module>
TypeError: not enough arguments for format string

Report •

#6
June 5, 2009 at 04:14:51
make sure you use template.txt and oldfile.txt as it is .

template.txt should look like this (with 3 question marks "?" )

template="""
<add key="Root" value="???"/>
<add key=.....>
<add key=>
<add key="dbName" value="???"/>
<add key="ApplicationName" value="???"/>

and oldfile.txt look like this:

<add key="Root" value="346"/>
<add key=.....>
<add key=>
<add key="dbName" value="livedb"/>
<add key="ApplicationName" value="live"/>


Report •

#7
June 8, 2009 at 01:38:17
Hi try to use following files to reproduce the error:

oldfile.txt
<xml version="1.0" encoding="utf-8"?>
<appSettings>

<add key="SQLMainConnString" value="server=10.10.10.10;database=MAIN2000SQL;uid=cust;pwd=test;"/>
<add key="SQLChannelConnString" value="server=10.10.10.10;uid=cust;pwd=test;database=firstbase;"/>
<add key="SQLWorkFlowConnString" value="server=10.10.10.10;database=WorkFlow;uid=cust;pwd=test;"/>
<add key="SQLFeedbackConnString" value="server=10.10.10.10;database=icFeedback2000SQL;uid=cust;pwd=test;"/>
<add key="SQLCostingConnString" value="server=10.10.10.10;database=firstbase;uid=cust;pwd=test;"/>
<add key="Root" value=""/>
<add key="QuickTranslate" value="false"/>
<add key="LanguageFiles" value="UKEnglish"/>
<add key="dbName" value="firstbase"/>
<add key="ApplicationName" value="firstbase"/>
<add key="ApplicationID" value="281"/>
<add key="DBServer" value="10.10.10.10"/>
<add key="DefaultACRHeadings" value="DB3D482E-BD57-4DE6-89F0-9333A06DDC09"/>
<add key="SQLMainPublisherConnString" value="server=10.10.10.10;database=main2000sql;uid=cust;pwd=test;"/>
<add key="SQLWorkFlowPublisherConnString" value="server=10.10.10.10;database=WorkFlow;uid=cust;pwd=test;"/>
<add key="SessionDSN" value="data source=10.10.10.10;initial catalog=SessionDB;persist security info=False;user id=cust;password=test;packet size=4096" />
<add key="LoginHandlerUrl" value="https://firstbase.test.com/BIW/Lobby/Authenticate.aspx" />
<add key="MailServer" value="localhost" />
<add key="SkinDirectory" value="Tskin" />

<add key="SQLWorkflowPersistence" value="Initial Catalog=WorkflowPersistence;Data Source=10.10.10.10;user=cust;password=test" />
<add key="SQLWorkflowTracking" value="Initial Catalog=WorkflowPersistence;Data Source=10.10.10.10;user=cust;password=test" />
<add key="HttpsSite" value="1"/>

</appSettings>

template.txt
<xml version="1.0" encoding="utf-8"?>
<appSettings>

<add key="SQLMainConnString" value="server=10.10.10.10;database=main2000sql;uid=cust;pwd=test;"/>
<add key="SQLChannelConnString" value="server=10.10.10.10;uid=cust;pwd=test;database=???;"/>
<add key="SQLWorkFlowConnString" value="server=10.10.10.10;database=WorkFlow;uid=cust;pwd=test;"/>
<add key="SQLFeedbackConnString" value="server=10.10.10.10;database=icFeedback2000SQL;uid=cust;pwd=test;"/>
<add key="SQLCostingConnString" value="server=10.10.10.10;database=???;uid=cust;pwd=test;"/>
<add key="Root" value=""/>
<add key="QuickTranslate" value="false"/>
<add key="LanguageFiles" value="UKEnglish"/>
<add key="dbName" value="???"/>
<add key="ApplicationName" value="???"/>
<add key="ApplicationID" value="???"/>
<add key="DBServer" value="10.10.10.10"/>
<add key="DefaultACRHeadings" value="DB3D482E-BD57-4DE6-89F0-9333A06DDC09"/>
<add key="SQLMainPublisherConnString" value="server=10.10.10.10;database=main2000sql;uid=cust;pwd=test;"/>
<add key="SQLWorkFlowPublisherConnString" value="server=10.10.10.10;database=WorkFlow;uid=cust;pwd=test;"/>
<add key="SQLFeedbackPublisherConnString" value="server=10.10.10.10;database=icFeedback2000SQL;uid=cust;pwd=test;"/>
<add key="LoginHandlerUrl" value="http://???.test.com/BIW/Lobby/Authenticate.aspx" />
<add key="MailServer" value="localhost" />
<add key="SkinDirectory" value="???" />
<add key="SessionDSN" value="data source=10.10.10.10;initial catalog=SessionDB;persist security info=False;user id=cust;password=test;packet size=4096" />

<add key="SQLWorkflowPersistence" value="Initial Catalog=WorkflowPersistence;Data Source=10.10.10.10;user=cust;password=test" />
<add key="SQLWorkflowTracking" value="Initial Catalog=WorkflowPersistence;Data Source=10.10.10.10;user=cust;password=test" />

<!-- <add key="HttpsSite" value="1"/> -->

</appSettings>


Here i want to replace "???" in template file with corresponding values in oldfile.


Report •

#8
June 8, 2009 at 03:22:09
then why didn't you present these actual samples in the first place?
import sys
oldfile = sys.argv[1]
template = sys.argv[2] 
toreplace={}
toreplace1={}
for line in open(template):
    line=line.strip()
    if "???" in line:
        ind=line.index("value")
        toreplace.setdefault(line[:ind],line)
for line in open(oldfile):
    line=line.strip()
    try:
        ind=line.index("value")
    except: pass
    else:
        if toreplace.has_key(line[:ind]):
            toreplace1[line[:ind]] =line
for line in open(template):
    line=line.strip()
    if "???" in line:
        ind=line.index("value")
        print toreplace1[line[:ind]]
    else:
        print line
<pre>

output
<pre>
C:\test>python test.py oldfile.txt template.txt
<xml version="1.0" encoding="utf-8"?>
<appSettings>

<add key="SQLMainConnString" value="server=10.10.10.10;database=main2000sql;uid=cust;pwd=test;"/>
<add key="SQLChannelConnString" value="server=10.10.10.10;uid=cust;pwd=test;database=firstbase;"/>
<add key="SQLWorkFlowConnString" value="server=10.10.10.10;database=WorkFlow;uid=cust;pwd=test;"/>
<add key="SQLFeedbackConnString" value="server=10.10.10.10;database=icFeedback2000SQL;uid=cust;pwd=test;"/>
<add key="SQLCostingConnString" value="server=10.10.10.10;database=firstbase;uid=cust;pwd=test;"/>
<add key="Root" value=""/>
<add key="QuickTranslate" value="false"/>
<add key="LanguageFiles" value="UKEnglish"/>
<add key="dbName" value="firstbase"/>
<add key="ApplicationName" value="firstbase"/>
<add key="ApplicationID" value="281"/>
<add key="DBServer" value="10.10.10.10"/>
<add key="DefaultACRHeadings" value="DB3D482E-BD57-4DE6-89F0-9333A06DDC09"/>
<add key="SQLMainPublisherConnString" value="server=10.10.10.10;database=main2000sql;uid=cust;pwd=test;"/>
<add key="SQLWorkFlowPublisherConnString" value="server=10.10.10.10;database=WorkFlow;uid=cust;pwd=test;"/>
<add key="SQLFeedbackPublisherConnString" value="server=10.10.10.10;database=icFeedback2000SQL;uid=cust;pwd=test;"/>
<add key="LoginHandlerUrl" value="https://firstbase.test.com/BIW/Lobby/Authenticate.aspx" />
<add key="MailServer" value="localhost" />
<add key="SkinDirectory" value="Tskin" />
<add key="SessionDSN" value="data source=10.10.10.10;initial catalog=SessionDB;persist security info=False;user id=cust;
password=test;packet size=4096" />

<add key="SQLWorkflowPersistence" value="Initial Catalog=WorkflowPersistence;Data Source=10.10.10.10;user=cust;password=
test" />
<add key="SQLWorkflowTracking" value="Initial Catalog=WorkflowPersistence;Data Source=10.10.10.10;user=cust;password=tes
t" />

<!-- <add key="HttpsSite" value="1"/> -->

</appSettings>

redirect to newfile using ">" as desired.

here's the executable...


Report •

#9
June 9, 2009 at 01:26:41
Hi,

Thanks for the excellent answer, really genius.
What i want is working.

One more requirement which i forgot earlier.
There are several tags which could be commented (flags) and that we have to pick from our old file. i.e. https flag which could be commented.

eg extract from above oldfile.txt
<add key="SQLWorkflowTracking" value="Initial Catalog=WorkflowPersistence;Data Source=10.10.10.10;user=cust;password=test" />
<add key="HttpsSite" value="1"/>

</appSettings>


template.txt
<add key="SQLWorkflowTracking" value="Initial Catalog=WorkflowPersistence;Data Source=10.10.10.10;user=cust;password=test" />

<!-- <add key="HttpsSite" value="1"/> -->

</appSettings>

In this case template.txt has 'Httpsite' tag as commented flag but it needs to be removed as oldfile.txt does not have it as a comment.

So what i would put in template is *** prefix i.e.
<!--***<add key="HttpsSite" value="1"/> -->
When 3 stars are found in the template than that line should be replaced by the one in 'oldfile.txt'.

I hope this will complete my all requirement.
Appriciate your reply.

Regards
Nehal



Report •


Ask Question