How to do simple, repetitive changes to XML

roidesfoux

Warlord
Joined
Nov 11, 2003
Messages
182
I've modified my civics, adding a sixth category. In order to get the game to work, I had to go through CIV4CivilizationInfos.xml and change

<InitialCivics>
<CivicType>CIVIC_DESPOTISM</CivicType>
<CivicType>CIVIC_BARBARISM</CivicType>
<CivicType>CIVIC_TRIBALISM</CivicType>
<CivicType>CIVIC_DECENTRALIZATION</CivicType>
<CivicType>CIVIC_PAGANISM</CivicType>
</InitialCivics>
<AnarchyCivics>
<CivicType>CIVIC_DESPOTISM</CivicType>
<CivicType>CIVIC_BARBARISM</CivicType>
<CivicType>CIVIC_TRIBALISM</CivicType>
<CivicType>CIVIC_DECENTRALIZATION</CivicType>
<CivicType>CIVIC_PAGANISM</CivicType>
</AnarchyCivics>

to

<InitialCivics>
<CivicType>CIVIC_DESPOTISM</CivicType>
<CivicType>CIVIC_BARBARISM</CivicType>
<CivicType>CIVIC_TRIBALISM</CivicType>
<CivicType>CIVIC_DECENTRALIZATION</CivicType>
<CivicType>CIVIC_PAGANISM</CivicType>
<CivicType>CIVIC_COMMUNAL_DEFENSE</CivicType>
</InitialCivics>
<AnarchyCivics>
<CivicType>CIVIC_DESPOTISM</CivicType>
<CivicType>CIVIC_BARBARISM</CivicType>
<CivicType>CIVIC_TRIBALISM</CivicType>
<CivicType>CIVIC_DECENTRALIZATION</CivicType>
<CivicType>CIVIC_PAGANISM</CivicType>
<CivicType>CIVIC_COMMUNAL_DEFENSE</CivicType>
</AnarchyCivics>

by hand 18 times. Now I'm thinking of downloading some new civs, but I'm highly unenthusiastic about doing this change again 19-65 times. I tried using MSWord for the "find and replace" function, but it wouldn't even open the file. Does anyone know an easy way to make this change?

-RdF
 
I'm sorry if this sounds rude or stupid (or both), but did you try saving the .XML file as a .TXT and then opening it up in Word? I think that will do what you want.
 
If you use Eclipse 3.1 with the XMLSpy plug-in, you have the option to find and replace all. Super simple, since I made a change similar to yours. Both are available for free, though the installation of Eclipse is a bit tricky.
Some basic instructions:
1) Install Java (needed to run and use Eclipse, since it is primarily a Java development tool) Instructions
Only go through step 16 if you want, no real need to test it.
2) Install Eclipse
Instructions
3) Install XMLspy home edition Link
4) Install the XMLspy Eclipse plugin (same link as #3)

Yeah it's a pain in the ass at first, but it will improve your programming 100%
compared to programming on word. Color-coding, auto-format, etc... It's soo pretty ;)
Plus if you plan on doing any programming (esp of java) for a career, it's a good idea to be familliar with Eclipse, lotsa companies use it.

Hope this helps
j
 
Rabbit_Alex: Neither rude nor stupid; it's best to start with the simplest advice. In this case, I did think to try that, but Word still recognized it as an XML file.

J_Period: I'll give it a try.

Thanks to both!

-RdF
 
J_Period, that XMLspy looks pretty cool. I'm gonna try it since I use Eclipse for my Java projects.
 
roidesfoux said:
Rabbit_Alex: Neither rude nor stupid; it's best to start with the simplest advice. In this case, I did think to try that, but Word still recognized it as an XML file.

J_Period: I'll give it a try.

Thanks to both!

-RdF

Word doesn't have the replace features you want anyway.

Personally, I wrote a simple little php program to do this sort of thing for me.

Code:
<?PHP

// File name to be query'd
$file = "C:\Program Files\Firaxis Games\Sid Meier's Civilization 4\Mods\Song of the moon\Assets\xml\civilizations\CIV4CivilizationInfos.xml";

$pattern =<<<_PAT_
			<AnarchyCivics>
_PAT_;

$replaceWith =<<<_REP_
			<AnarchyCivics>
				<CivicType>CIVIC_SCIENTIFIC</CivicType>
_REP_;

print "Replacing...";
print "\n";
print $file;
print "\n--------------------------------------\n";

$doc = file_get_contents($file);
#$doc = str_replace($pattern, $replaceWith, $doc);
$doc = ereg_replace($pattern, $replaceWith, $doc);
file_put_contents($file, $doc);
?>

I set the $pattern to what I want to find, and the $replaceWith to the replacement, $file to the file to change (which I get by copying the path from the explorer window--one step via context menu if you have the right shell extension). Because it's PHP, I can use multi-line input and output directly cut and pasted from the XML files in question. I can also use reg exp and {}s to capture and replace arbitrarily complex bits and pieces. The next best thing I've found after this is Dreamweaver :/.
 
Top Bottom