tags:

views:

60

answers:

2

If I have an xml file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<installerDefaults pathToAllUsers="C:\ProgramData\prog">
    <databaseConnector>
     <localDatabasePath>C:\ProgramData\prog\tracking.db3</localDatabasePath>
    </databaseConnector>
    <defaultLocales>
     <installerDefaultLocale>en-US</installerDefaultLocale>
    </defaultLocales>
    <directories>
     <languageDataBasePath>C:\ProgramData\prog\Content</languageDataBasePath>
    </directories>
</installerDefaults>

And I want to change the string "C:\ProgramData\prog" to something else in all three instances, how can I modify it so I only make one change and it changes all three? Actually, it'd be better if I could just change the "prog" and keep everything else.

I'm not using any language, I'm just editing the file in a text editor. Maybe what I'm asking isn't possible. Isn't there a way to concatenate strings, pseudo-xml e.g.

s = "C:\ProgramData\abcdefg\"

<installerDefaults pathToAllUsers = s>
<localDatabasePath>s + "tracking.db3"</localDatabasePath>
<languageDataBasePath>s + Content</languageDataBasePath>

So when the program that reads it in looks for the value of localDatabasePath, it get's C:\ProgramData\abcdefg\tracking.db3

Is that possible?

+1  A: 

It all depends on how rigorous you want to be. A simple Find/Replace in any text editor would do the job in this case. Text manupulation scripting languages such as sed or perl would let you do the same from the command line.

The problem with those approaches is that they won't readily extend to more general problems. Think about changing

C:\ProgramData\directories\Content

to

C:\ProgramData\wibble\Content

it's all too easy to also accidentally change the XML tag

<directories>

too. In general transformaing XML requires tools that understand XML.

Hence you really need something that understands XML, and that's where XSLT could be used. You can get a command line processor from here and there are tutorials for XSLT here.

That's quite a lot of learning, but if you are trying to develop skills it's setting you up to address more complex XML manipulation problems.

djna
A: 

Take a look at XML entities; e.g. as illustrated by this tutorial page

Entities allow you to define a value or an element structure in one place in the header of an XML file and use it at multiple points in the body. An XML file can also use entities defined elsewhere.

Stephen C