First adventure in XML

Published May 03, 2005
Advertisement
I recommended to The Forgotten Mindset that XML would be a good file format to use for his game data. You see, I recently read the article here that introduced me to TinyXml. What I ran into, and the only problem I had with that article, is that TinyXml seems best suited to stuffing lots of information into attributes and not the text elements between open and close tags. And while it works, most resources I've read have called it bad form.

For example, this is frowned upon:
"100"
direction="1" name="The first level">
"10" y="54" id="Enemy001">








while this seems okay:
	100	1	The first level			10		54		Enemy001	


While there's nothing stopping you from reading in the second example in with TinyXml, there's no mechanism included for searching for and converting the text nodes like those that exist for attributes. So, I set out to write it.

I could have (and may still) modify TinyXml's source to include the features I desire. For now, however, I've written a seperate utility-function module.

xmlutil.h
xmlutil.cpp
xmltest.cpp

Specifically, see the functions named FirstChildText, and NextSiblingText. I tried to follow the mechanisms built into TinyXml.

Each class that I want xml access for just needs to be smart enough to read and write itself to a TiXmlElement. Here's the code that loads and writes back out the second xml example above. (From xmltest.cpp)

int main(void){	TiXmlElement xmlElement("");	if(!xml::ReadXml("test.xml", xmlElement))	{		std::cout << "Couldn't read input file: test.xml\n";		return 1;	}	Level level;	if(!level.ReadXmlElement(xmlElement))	{		std::cout << "Couldn't read level\n";		return 1;	}	xmlElement = level.WriteXmlElement();	if(!xml::WriteXml("testoutput.xml", xmlElement))	{		std::cout << "Couldn't write output file: testoutput.xml\n";		return 0;	}	return 0;}
Next Entry Phase 3, profit
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement

Latest Entries

Advertisement