我有一个结构,我试图保存为xml,它似乎没有创建小型XML的问题,但是在较大的文件的情况下它似乎嵌套后面的标签,对于前30个左右我我的格式得到了正确的结果:
<argument><kind>2</kind><string>video.avi</string></argument><argument><kind>4</kind><string>0</string></argument>
然而,过了一段时间后,事情就开始了:
<argument><kind>5</kind><string/><argument><kind>2</kind><string>sprite.gif</string></argument></argument>
哪个不正确,参数不应该嵌套。
//Save an obiect as in xml/gm format
int save_obj(struct gm_object obj, char *file_str)
{
xmlTextWriterPtr writer;
xmlChar *tmp;
// Create a new XmlWriter for uri, with no compression.
writer = xmlNewTextWriterFilename(file_str, 0);
if (writer == NULL)
{
return 1; //1 = error
}
xmlTextWriterStartDocument(writer, NULL, "UTF-8", "no");
//Root
xmlTextWriterStartElement(writer, BAD_CAST "object");
xmlTextWriterWriteElement(writer, BAD_CAST "spriteName", BAD_CAST obj.spriteName);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "solid","%i",obj.solid);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "visible","%i",obj.visible);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "depth","%i",obj.depth);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "persistent","%i",obj.persistent);
xmlTextWriterWriteElement(writer, BAD_CAST "maskName", BAD_CAST obj.maskName);
xmlTextWriterWriteElement(writer, BAD_CAST "parentName", BAD_CAST obj.parentName);
xmlTextWriterStartElement(writer, BAD_CAST "events");
//This is where we store any ints that need to be converted to strings
char str_int_converted[(CHAR_BIT * sizeof(int) - 1) / 3 + 2];
int i;
for (i = 0; i<obj.event_count-1;i++)
{
xmlTextWriterStartElement(writer, BAD_CAST "event");
sprintf(str_int_converted, "%d", obj.events[i].enumb);
xmlTextWriterWriteAttribute(writer, BAD_CAST "enumb", BAD_CAST str_int_converted);
sprintf(str_int_converted, "%d", obj.events[i].eventtype);
xmlTextWriterWriteAttribute(writer, BAD_CAST "eventtype", BAD_CAST str_int_converted);
int ii;
for (ii = 0; ii<obj.events[i].action_count-1;ii++)
{
xmlTextWriterStartElement(writer, BAD_CAST "action");
xmlTextWriterWriteFormatElement(writer, BAD_CAST "libid", "%i", obj.events[i].actions[ii].libid);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "id","%i", obj.events[i].actions[ii].id);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "kind","%i", obj.events[i].actions[ii].kind);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "userelative","%i", obj.events[i].actions[ii].userelative);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "useapplyto", "%i", obj.events[i].actions[ii].useapplyto);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "isquestion", "%i", obj.events[i].actions[ii].isquestion);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "exetype","%i", obj.events[i].actions[ii].exetype);
xmlTextWriterWriteElement(writer, BAD_CAST "functionname", BAD_CAST obj.events[i].actions[ii].functionname);
xmlTextWriterWriteElement(writer, BAD_CAST "codestring", BAD_CAST obj.events[i].actions[ii].codestring);
xmlTextWriterWriteElement(writer, BAD_CAST "whoName", BAD_CAST obj.events[i].actions[ii].whoName);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "relative","%i", obj.events[i].actions[ii].relative);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "isnot","%i", obj.events[i].actions[ii].isnot);
xmlTextWriterStartElement(writer, BAD_CAST "arguments");
int iii;
for (iii = 0; iii<obj.events[i].actions[ii].arg_count-1;iii++)
{
//causing some of these to nest rather than properly close
xmlTextWriterStartElement(writer, BAD_CAST "argument");
xmlTextWriterWriteFormatElement(writer, BAD_CAST "kind", "%i", obj.events[i].actions[ii].arguments[iii].kind);
xmlTextWriterWriteElement(writer, BAD_CAST "string", obj.events[i].actions[ii].arguments[iii].string);
xmlTextWriterEndElement(writer); //Close <argument>
}
xmlTextWriterEndElement(writer); //Close <arguments>
xmlTextWriterEndElement(writer); //Close <action>
}
xmlTextWriterEndElement(writer); //Close <event>
}
xmlTextWriterEndElement(writer); //Close <events>
//Put physics stuff here
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObject","%i", obj.PhysicsObject);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectSensor","%i", obj.PhysicsObjectSensor);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectShape","%i", obj.PhysicsObjectShape);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectDensity","%f", obj.PhysicsObjectDensity);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectRestitution","%f", obj.PhysicsObjectRestitution);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectGroup","%i", obj.PhysicsObjectGroup);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectLinearDamping","%f", obj.PhysicsObjectLinearDamping);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectAngularDamping","%f", obj.PhysicsObjectAngularDamping);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectFriction","%f", obj.PhysicsObjectFriction);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectAwake","%i", obj.PhysicsObjectAwake);
xmlTextWriterWriteFormatElement(writer, BAD_CAST "PhysicsObjectKinematic","%i", obj.PhysicsObjectKinematic);
xmlTextWriterStartElement(writer, BAD_CAST "PhysicsShapePoints");
//This is where we store any floats that need to be converted to strings
char str_float_converted[(CHAR_BIT * sizeof(float) - 1) / 3 + 2];
for (i=0;i<obj.point_count;i++)
{
xmlTextWriterWriteFormatElement(writer, BAD_CAST "point","%i,%i", obj.PhysicsShapePoints[i].x, obj.PhysicsShapePoints[i].y);
}
xmlTextWriterEndElement(writer); //Close <PhysicsShapePoints>
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
}
以下是它生成的完整文件:http://pastebin.com/raw/iJHYWkWq 对不起我无法把它放在这篇文章的正文中因为它超过了字符限制。 我会非常感激任何帮助,我已经在这个bug工作了4个小时了,我似乎找不到其他人有这个问题,更别说修复它了。
答案 0 :(得分:2)
如果将NULL传递给它,则xmlTextWriterWriteElement()不能正常工作。在调用API之前验证字符串是否为NULL,或使用xmlTextWriterWriteFormatElement()和%s作为格式。