Click here to Skip to main content
12,700,201 members (35,643 online)
Click here to Skip to main content
Add your own
alternative version

Stats

35.2K views
11 bookmarked
Posted

Parsing XML on the iPhone

, 2 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Since I am developing a game engine for the iPhone that I would like to re-use, I have chosen to host all game data in the form of XML files

Introduction

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!

Read the original blog post here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Craig Giles
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 2 Pin
KarstenK26-Aug-13 3:14
memberKarstenK26-Aug-13 3:14 
GeneralPlease post the source code Pin
Magela21-Sep-10 0:09
memberMagela21-Sep-10 0:09 
GeneralMy vote of 4 Pin
baba lorenz18-Jul-10 23:47
memberbaba lorenz18-Jul-10 23:47 
GeneralMy vote of 1 Pin
KarstenK13-Jun-10 22:56
memberKarstenK13-Jun-10 22:56 
GeneralMy vote of 1 Pin
StuGuru3-Jun-10 5:34
memberStuGuru3-Jun-10 5:34 
GeneralTBXML V1.3 Pin
Thomas-Soft24-Feb-10 1:02
memberThomas-Soft24-Feb-10 1:02 
Generalvtd-xml Pin
Jimmy Zhang9-Jun-09 20:14
memberJimmy Zhang9-Jun-09 20:14 
GeneralNSXML Parser is read-only Pin
MixxerY2-Jun-09 22:13
memberMixxerY2-Jun-09 22:13 
GeneralRe: NSXML Parser is read-only Pin
ThinkMud4-Jun-09 16:21
memberThinkMud4-Jun-09 16:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 2 Jun 2009
Article Copyright 2009 by Craig Giles
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid