C++ XML Parsing: Suggestions on Approach for Parsing and Storing data

cclassparsingxml

I am looking into developing a C++ application to parse xml (using the rapidxml framework), and I would like some advice on how to approach this.

The file I want to parse is a XML game file that defines a set of addons for a starship ('outfits'), which have several attributes in common (like name and cost), but also fall into various categories.

For example:
Fixed weapons: range, rate of fire

Turret weapons, range, rate of fire, turret slew speed

Afterburners: acceleration modifier, fuel burn speed

etc etc etc

There are at least 5 classifications of outfits (and say 3 or 4 outfits that fall into each category), and some, like the weapons, have a few attributes in common with each other, but not with other categories like afterburners.

I want to write parsing code to handle each type of object and store its data in objects. I also want to minimize how much code I have to retype (i.e. make use of good OOP style so I can add new categories or tweak existing ones in the future).

What is the best way to approach this? I have already started the program, but I would like to have some input on the best approach.

Since this is a really broad question, I will give you some of my thoughts/specific questions so you can see what I am looking for:

  • Should I have a basic class called 'outfit' and use inheritance to customize the parsing for each specific category (i.e. 'fixed-weapons')? Should I have multiple levels of inheritance for similar categories (i.e. 'weapons'). How can I minimize code retyping so I only have to write the specific routines for each category?

  • Where should I handle the errors: in 'outfit', or in the derived classes? How much error handling do I need?

  • How can I easily access these objects later? Should I make a list-object with a vector containing each object? How can I access each object by name?

Any input is appreciated; you don't have to give a complete and comprehensive rundown on how to do this (though that would be nice :P).

Best Answer

  1. Each of your object types - Weapons for example should form a class hierarchy using inheritance. The attributes of this are known - and they must share the common attribute in the right way. (some times they are referred as entity objects).

  2. There should be only one class call it something like arsenal which will take up XML as an input and produce a list of objects as per the above definition. The output is constructed entity objects like any other object - you don't have to touch XML after that.

  3. Output of the arsenal can be a vector (template) to be able to access easily.

  4. Error handling : You haven't specified much about it, but as general rule, you should try to handle exceptions on per class basis i.e. each class should have its own rule set for type of exception they can generate. E.g. if rate_of_fire >MAX rate_of_fire = MAX : this should be done inside core class and application should not have to worry about it.

Related Topic