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).

3 Answers
3

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).

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.

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

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.

I would use CodeSynthesis to parse XML into an object (this can be templated to handle any CodeSynthesis classes) and then use these objects to create the actual game related objects with a builder class. These CodeSynthesis classes can then easily be serialsied back out to XML as and when required.

Try first writing the code or pseudo code that uses and manipulates the objects in the main body of the game. This may provide insights into what the object hierarchy should look like. It will at least validate your list+vector storage approach.

Deep class hierarchies are out of fashion - your app may be the exception but look at composing object with helper classes to reduce repetition of code.

Think about the builder pattern, where objects of another class are responsible for the XML parsing and return the main object fully formed. This will reduce the complexity of your main classes and be helpful if the storage format changes.