OpenCV中使用FileStorage创建和解析XML文件。但FileStorage不能处理节点为空的情况,且对属性也不友好。例子见这里。
libxml是C接口的处理xml文件的开源代码库。可以在这里下载。
对于一段如下的xml文件:
1
libxml处理的函数如下:
string getLabes(char *szDocName, map,Rect > &labels){ xmlDocPtr doc; xmlNodePtr curNode; xmlChar *szKey; string filename=""; doc = xmlReadFile(szDocName,"GB2312",XML_PARSE_RECOVER); //解析文件 if (doc==NULL){ fprintf(stderr,"Document not parsed successfully.\n"); return filename; } curNode = xmlDocGetRootElement(doc); //确定文档根元素 if (NULL == curNode){ fprintf(stderr,"empty document\n"); xmlFreeDoc(doc); return filename; } if (xmlStrcmp(curNode->name, BAD_CAST "Message")) { fprintf(stderr,"document of the wrong type, root node != Message"); xmlFreeDoc(doc); return filename; } curNode = curNode->xmlChildrenNode; xmlNodePtr propNodePtr = curNode; xmlNodePtr itemNodePtr; while(curNode != NULL){ //取出文件名称 if (xmlHasProp(curNode,BAD_CAST "mediaFile")) { propNodePtr = curNode; } if (!xmlStrcmp(curNode->name, BAD_CAST "Items")) { itemNodePtr = curNode->xmlChildrenNode; } curNode = curNode->next; } xmlAttrPtr attrPtr = propNodePtr->properties; while (attrPtr != NULL){ if (!xmlStrcmp(attrPtr->name, BAD_CAST "mediaFile")){ xmlChar* szAttr = xmlGetProp(propNodePtr,BAD_CAST "mediaFile"); char* szAttrG = u2g((char*)szAttr); filename = string(szAttrG); //cout<<"get filename: "< next; } // Item while (itemNodePtr != NULL){ int frameNum = 0; int peopleID = 0; Rect rect; // xmlAttrPtr attrPtr = itemNodePtr->properties; while (attrPtr != NULL){ if (!xmlStrcmp(attrPtr->name, BAD_CAST "frameNum")){ xmlChar* szAttr = xmlGetProp(itemNodePtr,BAD_CAST "frameNum"); frameNum = atoi((char*)szAttr); //cout<<"get frameNum: "< xmlChildrenNode; while (childNodePtr != NULL){ int l=0,t=0,r=0,b=0; attrPtr = childNodePtr->properties; while(attrPtr != NULL){ if (!xmlStrcmp(attrPtr->name, BAD_CAST "l")){ xmlChar* szAttr = xmlGetProp(childNodePtr,BAD_CAST "l"); l=atoi((char*)szAttr); xmlFree(szAttr); } if (!xmlStrcmp(attrPtr->name, BAD_CAST "t")){ xmlChar* szAttr = xmlGetProp(childNodePtr,BAD_CAST "t"); t=atoi((char*)szAttr); xmlFree(szAttr); } if (!xmlStrcmp(attrPtr->name, BAD_CAST "r")){ xmlChar* szAttr = xmlGetProp(childNodePtr,BAD_CAST "r"); r=atoi((char*)szAttr); xmlFree(szAttr); } if (!xmlStrcmp(attrPtr->name, BAD_CAST "b")){ xmlChar* szAttr = xmlGetProp(childNodePtr,BAD_CAST "b"); b=atoi((char*)szAttr); xmlFree(szAttr); } if (!xmlStrcmp(attrPtr->name, BAD_CAST "id")){ xmlChar* szAttr = xmlGetProp(childNodePtr,BAD_CAST "id"); peopleID=atoi((char*)szAttr); xmlFree(szAttr); } attrPtr = attrPtr->next; } if(!(l==0&&t==0&&b==0&&r==0)){ rect.x=l;rect.y=t;rect.width=r-l;rect.height=b-t; labels[pair (frameNum,peopleID)] = rect; //cout<<"get label: "<
for(map,Rect >::iterator it=labels.begin(); it!=labels.end();it++){ cout<<(*it).first.first<<" "<<(*it).first.second<<" " <<(*it).second.x<<" "<<(*it).second.y<<" " <<(*it).second.width<<" "<<(*it).second.height<
(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)
