Easy way to read XML: simple commenting with regular expressions

sprang

Warlord
Joined
Mar 11, 2005
Messages
197
Tired of paging down thru hundreds of lines of UnitInfos, or having to do a "Find" every time you want to jump to a new unit or building? Here's an easy way to make your XML files MUCH easier to read and navigate.

When you see too much of this, it gets hard on the eyes:
Code:
<!-- -->
<!-- Building Infos -->
<Civ4BuildingInfos xmlns="x-schema:CIV4BuildingsSchema.xml">
	<BuildingInfos>
		<BuildingInfo>
			<BuildingClass>BUILDINGCLASS_PALACE</BuildingClass>
			<Type>BUILDING_PALACE</Type>
			<SpecialBuildingType>NONE</SpecialBuildingType>
			<Description>TXT_KEY_BUILDING_PALACE</Description>
			<Civilopedia>TXT_KEY_BUILDING_PALACE_PEDIA</Civilopedia>
         		 .... hundred lines....
			<ImprovementFreeSpecialists/>
			<Flavors/>
			<HotKey/>
			<bAltDown>0</bAltDown>
			<bShiftDown>0</bShiftDown>
			<bCtrlDown>0</bCtrlDown>
			<iHotKeyPriority>0</iHotKeyPriority>
		</BuildingInfo>
		<BuildingInfo><!-- GREAT_PALACE -->
			<BuildingClass>BUILDINGCLASS_GREAT_PALACE</BuildingClass>
			<Type>BUILDING_GREAT_PALACE</Type>
			<SpecialBuildingType>NONE</SpecialBuildingType>
			<Description>TXT_KEY_BUILDING_GREAT_PALACE</Description>
			<Civilopedia>TXT_KEY_BUILDING_GREAT_PALACE_PEDIA</Civilopedia>

Use a text editor that supports "implicit" "folding" of the code files - i like the freebie opensource JEdit. It also colors the different types of text in the file: blue for code, black for values, red for comments, etc. So instead, you see this:

Code:
[COLOR="Red"]<!-- -->
<!-- Building Infos -->[/COLOR]
[COLOR="Blue"]<Civ4BuildingInfos xmlns=[COLOR="Magenta"]"x-schema:CIV4BuildingsSchema.xml"[/COLOR]>
	<BuildingInfos>
		<BuildingInfo> [COLOR="Black"][B][173 lines][/B][/COLOR]
		</BuildingInfo>
		<BuildingInfo> [COLOR="Black"][B][182 lines][/B][/COLOR]
		</BuildingInfo>
		<BuildingInfo> [COLOR="Black"][B][177 lines][/B][/COLOR]
		</BuildingInfo>
		<BuildingInfo> [COLOR="Black"][B][170 lines][/B][/COLOR]
		</BuildingInfo>[/COLOR]

Each building entry is now "folded" - hidden from sight. To read it, you can open it or hide it again. It also tells you how long each fold is. Great!

But which building is which? Here's the great part: a REGULAR EXPRESSION to search and insert (find and replace) the name of each building as a comment on the first line of the code paragraph! Expand all the folds ('Find' only works in the un-hidden code), and perform a Find and Replace with "Regular Expressions", using:

Find: (<BuildingInfo>)((?:\n\s+\S+\n\s+<Type>BUILDING_)(\w+)(?:</Type>))
Replace: $1<!-- $3 -->$2

The expression finds each instance of code that looks like this:

Code:
		[COLOR="Blue"]<BuildingInfo>
			<BuildingClass>[COLOR="Black"]BUILDINGCLASS_SomeRandomBuilding[/COLOR]</BuildingClass>
			<Type>[COLOR="Black"]BUILDING_SomeRandomBuilding[/COLOR]</Type>[/COLOR]

And replaces it with this:

Code:
		[COLOR="Blue"]<BuildingInfo>[COLOR="Red"]<!-- SomeRandomBuilding -->[/COLOR]
			<BuildingClass>[COLOR="Black"]BUILDINGCLASS_SomeRandomBuilding[/COLOR]E</BuildingClass>
			<Type>[COLOR="Black"]BUILDING_SomeRandomBuilding[/COLOR]</Type>[/COLOR]


Now, after you re-collapse the folds, you see this:

Code:
[COLOR="Red"]<!-- -->
<!-- Building Infos -->[/COLOR]
[COLOR="Blue"]<Civ4BuildingInfos xmlns=[COLOR="Magenta"]"x-schema:CIV4BuildingsSchema.xml"[/COLOR]>
		<BuildingInfo>[COLOR="Red"]<!-- PALACE -->[/COLOR] [COLOR="Black"][B][173 lines][/B][/COLOR]
		</BuildingInfo>
		<BuildingInfo>[COLOR="Red"]<!-- GREAT_PALACE -->[/COLOR] [COLOR="Black"][B][182 lines][/B][/COLOR]
		</BuildingInfo>
		<BuildingInfo>[COLOR="Red"]<!-- VERSAILLES -->[/COLOR] [COLOR="Black"][B][177 lines][/B][/COLOR]
		</BuildingInfo>
		<BuildingInfo>[COLOR="Red"]<!-- WALLS -->[/COLOR] [COLOR="Black"][B][170 lines][/B][/COLOR]
		</BuildingInfo>[/COLOR]

Cool! What about unit files? The entries for each unit are similar. Just alter the Find expression:

Find: (<UnitInfo>)((?:\n\s+\S+\n\s+<Type>UNIT_)(\w+)(?:</Type>))
Replace: $1<!-- $3 -->$2

Presto!
Code:
[COLOR="Red"]<!-- -->
<!-- Unit Infos -->[/COLOR]
[COLOR="Blue"]<Civ4UnitInfos xmlns=[COLOR="Magenta"]"x-schema:CIV4UnitSchema.xml"[/COLOR]>
	<UnitInfos>
		<UnitInfo>[COLOR="Red"]<!-- LION -->[/COLOR] [COLOR="Black"][178 lines][/COLOR]
		</UnitInfo>
		<UnitInfo>[COLOR="Red"]<!-- BEAR -->[/COLOR] [COLOR="Black"][170 lines][/COLOR]
		</UnitInfo>
		<UnitInfo>[COLOR="Red"]<!-- PANTHER -->[/COLOR] [COLOR="Black"][170 lines][/COLOR]
		</UnitInfo>
		<UnitInfo>[COLOR="Red"]<!-- WOLF -->[/COLOR] [COLOR="Black"][179 lines][/COLOR]
		</UnitInfo>[/COLOR]

Most other files have a slightly different syntax:

Code:
[COLOR="Blue"]	<PromotionInfos>
		<PromotionInfo>
			<Type>[COLOR="Black"]PROMOTION_COMBAT1[/COLOR]</Type>
			...more code....
		</PromotionInfo> [/COLOR]

So you can use a slightly altered expression:

Find: (<PromotionInfo>)((?:\n\s+<Type>PROMOTION_)(\w+)(?:</Type>))
Replace: $1<!-- $3 -->$2

...which gives you this:
Code:
[COLOR="Blue"]	<PromotionInfos>
		<PromotionInfo>[COLOR="Red"]<!-- COMBAT1 -->[/COLOR]
			<Type>[COLOR="Black"]PROMOTION_COMBAT1[/COLOR]</Type>
			...more code....
		</PromotionInfo> [/COLOR]

As always, try this on backed-up copies of your files first. "Regular expressions" are supported by LOTS of text editors and other programs, so use the editor of your choice and the expressions should still work. Hope this helps!
 
For quick and easy to XML editing try my XML editor. It can't edit every item, but can do most of them (close to 100%).

It's a lot easier to read than a text editor. For example when editing Civ4UnitInfos the units are listed on the left side and the data for the selected unit is listed on the right side. There are seperate search functions for each list.
 
Back
Top Bottom