TinyXML

Published May 05, 2006
Advertisement
Well, Deranged has pointed me to TinyXML, so I might just scrap the current file parser I'm working on. It depends on if I can use TinyXML to fill instances of class "item" with the information parsed from an XML file. Could anyone reading this comment and let me know if it's possible and a good idea to use TinyXML to parse an XML file like below (this is just a guess at what an XML file would look like, I don't have any experience with XML) into a C++ object like [also] below?

EDIT: Hah, the past three entries have started with "Well". Ironic, since I'm not feeling at all "well" right now!

  "Mana Potion"
>
A potion of Mana

+10

10
5
1





class item {    string name;    int price;    string desc;    mapint
> effects;
int InvenQuant, EquipQuant, StoreQuant;
}


Previous Entry Item loader
Next Entry Gone for a week
0 likes 1 comments

Comments

FrancisXavier
In case it's useful to you, you could try Daabli (http://sourceforge.net/projects/daabli/) for loading your C++ objects.

The code for loading your structure would look like this:
#include "Daabli.h"

class Item
{
    std::string                 _name;
    int                         _price;
    std::string                 _desc;
    std::map<std::string, int>  _effects;
    int                         _invenQuant;
    int                         _equipQuant;
    int                         _storeQuant;

public:
    const bool Read(Daabli::Reader &r)
    {
        return
            r.Read( "name",      _name          ) &&    // required
            r.Read( "price",     _price,      0 ) &&    // optional
            r.Read( "desc",      _desc          ) &&    // required
            r.Read( "effects",   _effects       ) &&    // required
            r.Read( "inventory", _invenQuant, 0 ) &&    // optional
            r.Read( "equipment", _equipQuant, 0 ) &&    // optional
            r.Read( "store",     _storeQuant, 0 );      // optional
    }
};

int main(int /*argc*/, char * /*argv*/[])
{
    Daabli::Reader r;

    if( !r.FromFile( "input.txt" ) )
        return -1;

    Item item;

    // Read the item
    if( !r.Read( "item", item ) )
        return -1;

    // Do something with item here

    return 0;
}


And the input.txt data file would look like this:
item =
{
    name = "Mana Potion";
    desc = "A potion of Mana";
    effects =
    {
        { "mana", 10 },
        { "life", 1 }   // gives some life also
    };
    inventory = 5;
    equipment = 1;
    store     = 10;
};


For more information about Daabli, see this thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=539326
July 17, 2009 06:15 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement