TinyXml - Parsing Gives Bad Strings

I'm trying to write a game that uses events inside of XML-files. I made the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void do_event( string filename, int eventindex ) { //code for loading events from xml
    string listedevent;
    string cur_action;
    string cur_value;
    TiXmlDocument doc ( filename.c_str() );
    if (doc.LoadFile()) {
        //get root element of doc
        TiXmlElement *root = doc.RootElement();

        for( TiXmlElement* event = root->FirstChildElement();
                event; event = event->NextSiblingElement())
        {
            //get the index
            listedevent = event->Attribute("index");
            //if the event is the one we're searching for, do it
            if ( atoi( listedevent.c_str() ) == eventindex ) {
                //get flags
                for( TiXmlElement* flag = root->FirstChildElement();
                        flag; flag = flag->NextSiblingElement())
                {
                     Beep( 800, 500 );
                    //set values for flag
                    cur_action = flag->Attribute("action");
                    cur_value = flag->Attribute("value");
                    Beep( 500, 500 );
                    //do flag's action
                    do_action( cur_action, cur_value );
                }
            }
        }
    }

}

This is called with:
do_event( "bin/bin.ys", 0000 ); //do the first event of the game: 0000
bin/bin.ys looks like this:
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0"?>
<events>

  <event index="0000">

    <flag action="map_load" value = "res/map/menu/main/main.ym" />

  </event>

</events>

I know the program gets as far as
1
2
cur_action = flag->Attribute("action");
cur_value = flag->Attribute("value");

because of the beeps that play. The program closes after playing the first beep. Anybody know why the program exits at this point? Or at least a way to fix this?
You look for your first flag as a child of the root element instead of the current event element, so as you don't specifity a node name in FirstChildelement, it returns you the events node and not a flag node.
As you have the wrong node type, the Attributes() will return NULL, and string crashes crashes when constructed with a NULL char* as argument.

If you pass STL strings to tinyxml, compile tinyxml with STL string support, it will remove the need for c_str() at line 5 for exemple
Woops. Didn't see that. Thanks.
Topic archived. No new replies allowed.