Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: Trouble loading long string from XML file using Mini-XML
    text
    copied!<p>I'm using the Mini-XML library to parse and XML file.</p> <p>I am able to load just about every element and attribute, but I am having trouble loading a long string.</p> <p>Here is the relevant part of the code:</p> <pre><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 &lt;Systems&gt; node Asset_elem = mxmlWalkNext(tree, tree, MXML_DESCEND_FIRST); //Start loading &lt;asset&gt; 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-&gt;child-&gt;value.text.string; } else{ tmp_str = NULL; } delete tmp_str; delete temp_sub_node; </code></pre> <p>Here is one element that I need to parse:</p> <pre><code>&lt;asset name="Ammu"&gt; &lt;pos&gt; &lt;x&gt;90.000000&lt;/x&gt; &lt;y&gt;2490.000000&lt;/y&gt; &lt;/pos&gt; &lt;GFX&gt; &lt;space&gt;A00.png&lt;/space&gt; &lt;exterior&gt;lava.png&lt;/exterior&gt; &lt;/GFX&gt; &lt;presence&gt; &lt;faction&gt;Empire&lt;/faction&gt; &lt;value&gt;100.000000&lt;/value&gt; &lt;range&gt;2&lt;/range&gt; &lt;/presence&gt; &lt;general&gt; &lt;class&gt;A&lt;/class&gt; &lt;population&gt;60000&lt;/population&gt; &lt;services&gt; &lt;land/&gt; &lt;refuel/&gt; &lt;bar/&gt; &lt;missions/&gt; &lt;commodity/&gt; &lt;outfits/&gt; &lt;/services&gt; &lt;commodities&gt; &lt;commodity&gt;Food&lt;/commodity&gt; &lt;commodity&gt;Ore&lt;/commodity&gt; &lt;commodity&gt;Industrial Goods&lt;/commodity&gt; &lt;/commodities&gt; &lt;description&gt;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.&lt;/description&gt; &lt;bar&gt;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.&lt;/bar&gt; &lt;/general&gt; &lt;tech&gt; &lt;item&gt;Basic Outfits 1&lt;/item&gt; &lt;/tech&gt; &lt;/asset&gt; </code></pre> <p>I am only getting the first word from the description tag.</p> <p>Why?</p> <p>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.</p> <p>Any suggestions?</p> <p>EDIT 2: I changed the callback to OPAQUE so that it would ignore whitespaces, but now it just returns NULL.</p> <p>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.</p> <p>EDIT 4: I even removed all but one "asset" object, and then its "bar" element, and it still crashes. This is absolutely bizarre!</p> <p>EDIT 5: Okay, I isolated the problem piece of code:</p> <pre><code>tmp_str = temp_sub_node-&gt;child-&gt;value.opaque; </code></pre> <p>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?</p> <p>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?!</p> <p>EDIT 7: FINALLY! Figured it out. I changed MXML_DESCEND to MXML_DESCEND_FIRST, and it works fine. WHEW!!!! What a relief. Thanks guys!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload