views:

52

answers:

2

I'm using the Mini-XML library to parse and XML file.

I am able to load just about every element and attribute, but I am having trouble loading a long string.

Here is the relevant part of the code:

//Load XML file into XmlO
    void load(wxString filenam){
        //First, convert wxString to std::string for safety (char* is transient), then to const char*
        std::string tmp_filenam = std::string(filenam.mb_str());
        const char* tmp_filenam2 = tmp_filenam.c_str();
        //Get pointer to file
        fp = fopen(tmp_filenam2,"r");
        //Load tree
        tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);

        //Close file (be nice!)
        fclose(fp);

        //Load <Systems> node
        Asset_elem = mxmlWalkNext(tree, tree, MXML_DESCEND_FIRST);

        //Start loading <asset> elements

        //Temporary Elements
        mxml_node_t *node; //Node to save
        mxml_node_t *subnode_pos; //Subnode for pos nodes
        mxml_node_t *subnode_GFX; //Subnode for GFX nodes
        mxml_node_t *subnode_pres; //Subnode for presence nodes
        mxml_node_t *subnode_gen; //Subnode for general nodes
        mxml_node_t *subnode_serv; //Subnode for services nodes
        mxml_node_t *subnode; //Subnode
        const char* name_tmp; //String for names of asset
        const char* tmp_str; //String for anything :P
        float x_pos; //X_pos Float
        float y_pos; //Y_pos Float
        const char* gfx_space;
        const char* gfx_ext;
        const char* pres_fac;
        float pres_val;
        int pres_range;
        const char* plan_class;
        int population;
        bool land;
        bool refuel;
        bool bar;
        bool missions;
        bool commodity;
        bool outfits;
        bool shipyard;
        const char* descrip;
        const char* bar_descrip;

        //Load first asset
        node = mxmlFindElement(Asset_elem, tree, "asset", NULL, NULL, MXML_DESCEND);
        //Start loading the rest of the ssys elements (but fail if first element is NULL)
        int i = 1;
        while (node != NULL){
            //Load name attrib
            name_tmp = mxmlElementGetAttr(node, "name");

            //Mark Branching nodes
            //Pos Element
            subnode_pos = mxmlFindElement(node, Asset_elem, "pos", NULL, NULL, MXML_DESCEND);
            //GFX Element
            subnode_GFX = mxmlFindElement(node, Asset_elem, "GFX", NULL, NULL, MXML_DESCEND);
            //Presence Element
            subnode_pres = mxmlFindElement(node, Asset_elem, "presence", NULL, NULL, MXML_DESCEND);
            //General Element
            subnode_gen = mxmlFindElement(node, Asset_elem, "general", NULL, NULL, MXML_DESCEND);
            //Services Sub-element
            subnode_serv = mxmlFindElement(subnode_gen, Asset_elem, "services", NULL, NULL, MXML_DESCEND);

/*********Loading routines that work********/

            //Get Descriptions

            const char * tmp_str;
            mxml_node_t *temp_sub_node;
            temp_sub_node = mxmlFindElement(subnode_gen, subnode_gen, "description", NULL, NULL, MXML_DESCEND);
            if(temp_sub_node != NULL){
                tmp_str = temp_sub_node->child->value.text.string;
            }
            else{
                tmp_str = NULL;
            }
        delete tmp_str;
        delete temp_sub_node;

Here is one element that I need to parse:

<asset name="Ammu">
  <pos>
   <x>90.000000</x>
   <y>2490.000000</y>
  </pos>
  <GFX>
   <space>A00.png</space>
   <exterior>lava.png</exterior>
  </GFX>
  <presence>
   <faction>Empire</faction>
   <value>100.000000</value>
   <range>2</range>
  </presence>
  <general>
   <class>A</class>
   <population>60000</population>
   <services>
    <land/>
    <refuel/>
    <bar/>
    <missions/>
    <commodity/>
    <outfits/>
   </services>
   <commodities>
    <commodity>Food</commodity>
    <commodity>Ore</commodity>
    <commodity>Industrial Goods</commodity>
   </commodities>
   <description>Ammu is a generally calm planet, once one is accustomed to the constant rumbling of the lava flows. Lava eruptions are often felt in the subterranean spaceport, but the way it doesn't seem to phase the locals reassures you.</description>
   <bar>The Ammu Spaceport Bar, known as "The Heatsink" due to its frigid temperatures, in contrast to the rest of the station. While primarily known for their temperature, that's not to say they can't whip up a mean Pan-Galactic Gargle Blaster.</bar>
  </general>
  <tech>
   <item>Basic Outfits 1</item>
  </tech>
 </asset>

I am only getting the first word from the description tag.

Why?

EDIT 1: I tried switching over to std::string, but the MiniXML library is returning a const char*, which apparently cannot hold such a long string.

Any suggestions?

EDIT 2: I changed the callback to OPAQUE so that it would ignore whitespaces, but now it just returns NULL.

EDIT 3: I now changed the methods to acquire value.opaque instead of value.text.string. This makes the "description" tag work great, but the "bar" tag still crashes when I try to load it into a const char*. I tried removing quotes and the like from the xml file to see if that was causing it, but it didn't help.

EDIT 4: I even removed all but one "asset" object, and then its "bar" element, and it still crashes. This is absolutely bizarre!

EDIT 5: Okay, I isolated the problem piece of code:

tmp_str = temp_sub_node->child->value.opaque;

However, I have this integrated into a method, the same method I am using for the description element (which directly precedes it), and that works fine. What is wrong?

EDIT 6: Oddly enough, when I change the search string to "bar ", it fails gracefully (i.e. returns NULL). It's only when I change it to "bar" (the element I need) that it crashes. Is this a reserved keyword or something that mini xml doesn't like?!

EDIT 7: FINALLY! Figured it out. I changed MXML_DESCEND to MXML_DESCEND_FIRST, and it works fine. WHEW!!!! What a relief. Thanks guys!

+1  A: 

If you are using C++ then for string handling "string" STL class can be used. It can load any number of characters up to the memory limit.

qasimzee
Does const char* have a limit other than memory?
Biosci3c
+1  A: 

You need to replace: tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);

By:

tree = mxmlLoadFile(NULL, fp, MXML_OPAQUE_CALLBACK);

Is that what you tried?

I think you also need to read value like

tmp_str = temp_sub_node->child->value.opaque;

Benoit Thiery
Yay, one of them is working. Gonna try the other now.
Biosci3c
Okay, having another problem, see edits above.
Biosci3c
Some bounty will be coming your way :).
Biosci3c