我使用Mini-XML库来解析和XML文件。
我可以加载几乎所有的元素和属性,但我在加载一个长字符串时遇到了问题。
以下是代码的相关部分:
//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;下面是我需要解析的一个元素:
<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>我只从description标签中得到第一个单词。
为什么?
编辑1:我尝试切换到std:: string,但是MiniXML库返回一个const char*,它显然不能容纳这么长的字符串。
有什么建议吗?
编辑2:我将回调改为不透明,这样它就会忽略空格,但现在它只返回NULL。
编辑3:我现在更改了方法,以获取value.opaque而不是value.text.string。这使得"description“标签工作得很好,但是当我试图将"bar”标签加载到const char*中时,它仍然崩溃了。我试着从xml文件中删除引号之类的东西,看看是不是这引起的,但是没有帮助。
编辑4:我甚至删除了所有的“资产”对象,然后删除了它的"bar“元素,但它仍然崩溃。这绝对是奇怪的!
编辑5:好的,我隔离了有问题的代码:
tmp_str = temp_sub_node->child->value.opaque;但是,我将其集成到一个方法中,该方法与我对description元素(直接在它之前)使用的方法相同,而且工作得很好。怎么啦?
编辑6:奇怪的是,当我将搜索字符串改为"bar“时,它优雅地失败了(即返回NULL)。只有当我将它更改为"bar“(我需要的元素)时,它才会崩溃。这是保留关键字还是mini xml不喜欢的关键字?!
编辑7:最后!我想通了。我将MXML_DESCEND更改为MXML_DESCEND_FIRST,它工作得很好。呼!啊!可以松口气了。谢谢你们!
发布于 2010-10-23 05:09:11
需要替换:mxmlLoadFile=MXML_TEXT_CALLBACK(NULL,fp,tree );
出自:
树= mxmlLoadFile(NULL,fp,MXML_OPAQUE_CALLBACK);
这就是你想要的吗?
我认为你还需要像这样读取值
tmp_str =temp_sub_node->子级->值.不透明;
发布于 2010-10-20 12:58:03
如果您使用的是C++,那么对于字符串处理,可以使用" string“STL类。它可以加载任意数量的字符,直到内存限制。
https://stackoverflow.com/questions/3974763
复制相似问题