I can't tell if I'm not getting the grand idea from XML or what but I'm confused as hell. I spruced up some code for one of my games to load through an XML configuration file and this is what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
TiXmlDocument settings( filename.c_str() ); //wtf this is supposed to allow std::string but doesn't. I'm working on it...
std::cerr << "Loading configuration file..." << std::endl;
if (settings.LoadFile())
{
for (TiXmlElement *i = settings.FirstChildElement(); strcmp(i->Value, "settings") != 0; i = i->NextSiblingElement();) //Bullshit not STL library...
{
if (!i)
{
std::cerr << "Failed to find settings. Creating default settings template." << std::endl;
break; //If null or 0 (whichever...), break for loop.
}
}
}
std::cerr << "Failed to load configuration file. Fuck me..." << std::endl; //Very basic error checking until I find something better.
As you can see, I'm not happy about most of this for obvious reasons. 1) It looks sloppy which I'm sure I can fix with some cleaning up afterward. 2) I have no clue if I'm doing this correctly lol. This is my first time attempting to parse an XML file and to be honest, I don't like TinyXML's little tutorial.
My problem to be solved: Is this a common way to go about things? I'm trying to search for settings and simply place the values of the attributes of the element into a some variables of a class. I'm not even going to worry about if the values in the XML are blank for right now since this is holding up my project.
Well, my first question would be if you really need XML. If you just need a simple configurations file you could go with INI or your own format. I generally use something like this when I just need to store simple data: variable = value0 [value1 [...]]
value0-valueN make up a vector, and their types are weak (http://en.wikipedia.org/wiki/Weak_typing ).
Both this and INI are far simple to understand and parse than XML.
Well, I partially wanted to do this as an experience. I personally believe that INI is far more convenient and less complex for the common person but since everyone loves XML apparently, I figured I ought to learn a thing or two. I understand the concept behind it , but I simply don't understand the libraries that take advantage of it.
I tried TiCPP. It seems the public header won't compile under vs2008 or mingw. Also, looking at the header where the errors were indicated, I found nothing wrong with it. It magically errors half way down the page on the most random of functions.
vs2008 errors out on this (and about 30 others just as similar): StylesheetReference( TiXmlStylesheetReference* stylesheetReference );
The error is this:
error C2535: 'ticpp::StylesheetReference::StylesheetReference(void)' : member function already defined or declared
That makes no sense to me at all as to why it thinks it's void parameters. If I don't figure this out in a few moments, I'm probably going to either go back to INI or move to a different XML library...
You might want to check out libxml later. It's a bit hard to build a .lib to use with VC++, since there isn't a project and, to be honest, I haven't figured out how to build with MinGW so that the .lib works with any compiler, so that means you have to add the sources to a new project yourself. But it's pretty easy to use, I learnt by just reading a bit of the tutorial and following the tree structure in the debugger. XML is complicated.