Archive for March, 2009

Code Snippet of the Week: Using Lua to parse Xml

Wednesday, March 18th, 2009

Many game studios these days, prefer to integrate some kind of generic scripting language into their game engine. There are several reasons for this. Perhaps the most convincing reason is that when your game’s code base becomes large, the compiling time also increases which in turn subtracts develop time. When you want to augment your game with new updates, perhaps you should elect to integrate a game scripting api into your engine.

Lua is becoming the game scripting language of choice more and more and rather large game companies such as Lucas Arts and Blizzard with World of Warcraft. It is a very simple scripting language that can impact your game exponentially. Today, I would like to show you a short snippet of how your can parse xml using lua. Lua has a very light weight version of regular expressions. LUa is suppose to be small so it doesn’t support the full power of regular expressions but what it does support is normally more than you need anyways. If you understand regular expressions, this tutorial may make more since. You may want to brush up on your regular expressions here.

When parsing an xml with a large amount of records, it is usually a good idea to include the number of records there are in the xml feed. The following method demonstrates how to do that. The strXmlData variable acts as a string which contains the xml feed.
The objContainer variable is an empty table set that will contain our values. The first tag we will parse out is the “TotalNumberRecords” tag which will tell us how many records we have. The search pattern “(%d+)” means that we want to search for 1 or more numbers.

function parseXmlHeader(strXmlData, objContainer)
objContainer.NumberRecords = parseDataByTag(strXmlData, "NumberRecords", "(%d+)")

Next we need to define the parseDataByTag. This method will simply find the first instance of a given tag name and return its value.

function parseDataByTag(strXmlData, strTag, strPattern)
	local strStartTag = "<" .. strTag .. ">"
	local strEndTag = "</" .. strTag .. ">"
	local strPattern = strStartTag..strPattern..strEndTag
	local iStart, iEnd, strData = string.find(strXmlData, strPattern)
	return strData

As you can see, we are building our xml tag using the tag name and simply using lua built in string library to apply our search pattern to the xml feed. Finally, we need one more method that will look for the same tag name multiple times and fill in our data structure or object container.

function fillInDataByTag(strXmlData, objContainer, strTag, strPattern)
	local strStartTag = "<" .. strTag .. ">"
	local strEndTag = "</" .. strTag .. ">"
	strPattern = strStartTag..strPattern..strEndTag
	local iStart = 0
	local iEnd = 0
	local strData = ""
	local iTableIndex = 0
	for i=0, objContainer.NumberRecords-1  do
	    iStart, iEnd, strData = string.find(strXmlData, strPattern, iEnd)
	    if objContainer[iTableIndex] == nil then
	        objContainer[iTableIndex] = {}
		if strData ~= nil then
	    	objContainer[iTableIndex][strTag] = string.sub(strData,1,25)--strData
	    	objContainer[iTableIndex][strTag] = ""
        iTableIndex = iTableIndex + 1
	    if iEnd == nil then

So lets say you want to create a data structure in Lua that will get populated by an xml feed. Using the following code, you could do something like the following.

strXmlData = getXmlFeed()
parseXmlHeader(strXmlData, objContainer)
fillInDataByTag(g_objectData, objContainer,"player_health", "(.-)")
print("Player 1's health is: "..objContainer[0].player_health)
print("Player 2's health is: "..objContainer[1].player_health)

The “(.-)” search pattern just means you want to search for any type of character using a non greedy approach.
Pretty simple huh? Well this week’s code snippet was short but hopefully it will give you an incentive to investigate Lua or other scripting language apis and to utilize their power. If you would like to learn more about Lua, check out the homepage at You can also purchase their wonderful reference guide at Amazon.

Did you find this code snippet useful?

View Results

Loading ... Loading ...