Advertisement Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

First adventure in XML

Sign in to follow this  
Jesse Chounard


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:

The first level


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.


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;

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;

Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!