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

First adventure in XML

Sign in to follow this  
Jesse Chounard

276 views

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;
}

Sign in to follow this  


0 Comments


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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!