C libxml2 xmlTextWriter 标签随机嵌套
C libxml2 xmlTextWriter tags nest themselves randomly
我有一个结构,我试图将其另存为 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
抱歉,我无法将其放入此 post 的正文中,因为它超出了字符数限制。
如果有任何帮助,我将不胜感激,我已经在这个错误上工作了 4 个小时,而且我似乎找不到其他人遇到过这个问题,更不用说修复它了。
如果传递 NULL,xmlTextWriterWriteElement() 将无法正常工作。在调用 API 或使用 xmlTextWriterWriteFormatElement() 和 %s 作为格式之前验证您的字符串不为 NULL。
我有一个结构,我试图将其另存为 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 抱歉,我无法将其放入此 post 的正文中,因为它超出了字符数限制。 如果有任何帮助,我将不胜感激,我已经在这个错误上工作了 4 个小时,而且我似乎找不到其他人遇到过这个问题,更不用说修复它了。
如果传递 NULL,xmlTextWriterWriteElement() 将无法正常工作。在调用 API 或使用 xmlTextWriterWriteFormatElement() 和 %s 作为格式之前验证您的字符串不为 NULL。