Parsing XML on the iPhone

Since I am developing a game engine that I would like to re-use, I have chosen to host all game data in the form of XML files. There are various reasons for doing this, partly because I can make game editors (Map Editor, Item Editor, NPC Editor, etc) and partly because I value using re-useable code. 

In order to get it all working, I had to explore the NSXMLParser given by the iPhone SDK. With XNA and the .NET framework, I got used to reading in XML in a very specific way, and the iPhone does it quite differently. Currently, my test map’s XML file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Map>
	<MapName>Test Map</MapName>
	<MapContentName>Map001</MapContentName>
	<MapDimensions>
		<MapWidth>5</MapWidth>
		<MapHeight>5</MapHeight>
	</MapDimensions>	

	<TileDimensions>
		<TileWidth>32</TileWidth>
		<TileHeight>32</TileHeight>
	</TileDimensions>	

	<SpriteSheetName></SpriteSheetName>
	<MapLayers>
		<BaseLayer>0, 1, 0, 0, 0, 0, 0, 0, 0</BaseLayer>
		<MiddleLayer>0, 1, 0, 0, 0, 0, 0, 0, 0</MiddleLayer>
		<TopLayer>0, 1, 0, 0, 0, 0, 0, 0, 0</TopLayer>
		<AtmosphereLayer>0, 1, 0, 0, 0, 0, 0, 0, 0</AtmosphereLayer>
		<CollisionLayer>0, 0, 0, 0, 0, 0, 0, 0, 0</CollisionLayer>
	</MapLayers>
</Map>

In order to read in the XML, there are 3 methods that you need to implement. Since your XML parser goes line by line, you will need to write a method that starts an element, ends an element, and reads a character. Such as:

// Start of element
- (void)parser:(NSXMLParser *)parser 
			didStartElement:(NSString *)elementName 
			namespaceURI:(NSString *)namespaceURI 
			qualifiedName:(NSString *)qName 
			attributes:(NSDictionary *)attributeDict
{
}

// Found Character
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string 
{
}

// End Element
- (void) parser:(NSXMLParser *)parser 
			didEndElement:(NSString *)elementName 
			namespaceURI:(NSString *)namespaceURI 
			qualifiedName:(NSString *)qName
{
}

Once you have these methods set up, all you really need to do is populate them. For instance, in the “Start Element” method, if your “elementName” = “MapName” (in the above XML case) you would allocate the map classes NSString like so;

mapName = [NSMutableString string];

When you found a character, you would store those into a string, and when the end of the element is reached, you would put that string into whatever element it was reading. For example, the map name:

mapName = stringValueFromFoundCharacter;

Not too hard, but wasn’t easy to figure out either.. At least I know it and now can implement readers for every data that I wish to store as XML!

About these ads

13 comments so far

  1. [...] Parsing xml on the iPhone: This tutorial will show you how to parse XML using the iPhone SDK. [Craig Giles] [...]

  2. [...] Parsing xml on the iPhone: This tutorial will show you how to parse XML using the iPhone SDK. [Craig Giles] [...]

  3. [...] Parsing xml on the iPhone: This tutorial will show you how to parse XML using the iPhone SDK. [Craig Giles] [...]

  4. [...] Parsing xml on the iPhone: This tutorial will show you how to parse XML using the iPhone SDK. [Craig Giles] [...]

  5. [...] Parsing xml on the iPhone: This tutorial will show you how to parse XML using the iPhone SDK. [Craig Giles] [...]

  6. Web Design Help on

    [...] Parsing xml on the iPhone: This tutorial will show you how to parse XML using the iPhone SDK. [Craig Giles] [...]

  7. Tom Bradley on

    Check out http://www.tbxml.co.uk for a super-fast, lightweight, easy to use XML parser!

  8. [...] Parsing XML on the iPhone Another great look at how to parse XML within iPhone apps. [...]

  9. [...] Parsing XML on the iPhone Another great look at how to parse XML within iPhone apps. [...]

  10. [...] Parsing XML on the iPhone Another great look at how to parse XML within iPhone apps. [...]

  11. Usdating on

    Thanx for the valuable information. This was just the thing I was looking for, I really like how it includes the actual curved shape flight paths. keep posting. Will be visiting back soon.

  12. Sadi on

    Can any one help me to know how can i load 30- 50 data at a time from a huge xml file in iphone and then when user want to go to other page then it will load another 30-50. i mean pagination. Please please help.

  13. anop72 on

    it very simple thank for post


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: