opencv 读写XML YML

前端之家收集整理的这篇文章主要介绍了opencv 读写XML YML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//序列没有标签
	CvMemStorage *mem = cvCreateMemStorage(0);
	CvFileStorage *file = cvOpenFileStorage("e:\\123.yml",mem,CV_STORAGE_READ | CV_STORAGE_WRITE);
	cvStartWriteStruct(file,NULL,CV_NODE_SEQ );

	cvWriteInt(file,123);//写入的是序列,名称都为NULL
	cvWriteReal(file,32.0);
	cvWriteString(file,"123123");
	
	int dbData[5] = {12,3,5,8};
	const string strType = "i";
	cvWriteRawData(file,dbData,strType.c_str());
	cvWriteComment(file,"my comment",0);//把注释写在该行的后面

	CvFileNode *node = cvGetRootFileNode(file,0);
	if (node != NULL){
		printf("%s\n",node->data.str.ptr);
	}

	cvEndWriteStruct(file);
	cvReleaseFileStorage(&file);
	cvReleaseMemStorage(&mem);

可以看出序列写文件没有节点

CvMemStorage *mem = cvCreateMemStorage(0);
	CvFileStorage *file = cvOpenFileStorage("e:\\123.xml","UserInfo",CV_NODE_MAP );

	cvWriteInt(file,"Integer","Real","String","123123");

	cvWriteComment(file,0);//把注释写在该行的后面
	cvEndWriteStruct(file);

	cvReleaseFileStorage(&file);
	cvReleaseMemStorage(&mem);


如果是MAP,就可以了

代码如下

CvMemStorage *mem1 = cvCreateMemStorage(0);
	CvFileStorage *file1 = cvOpenFileStorage("e:\\123.xml",mem1,CV_STORAGE_READ);

	CvStringHashNode *hashNode = cvGetHashedKey(file1,strlen("UserInfo"));
	CvFileNode *node1 = cvGetFileNode(file1,hashNode);
	if (node1 != NULL){//获取根节点名称
		printf("%s\n",cvGetFileNodeName(node1));
	}

	hashNode = cvGetHashedKey(file1,strlen("Real"));
	CvFileNode *nodeReal = cvGetFileNode(file1,node1,hashNode);
	if (nodeReal != NULL){//获取子节点名称
		printf("%s\n",cvGetFileNodeName(nodeReal));
		printf("%d\n",cvReadInt(nodeReal));
	}

	hashNode = cvGetHashedKey(file1,strlen("Integer"));
	CvFileNode *nodeInteger = cvGetFileNode(file1,hashNode);
	if (nodeInteger != NULL){//获取子节点名称
		printf("%s\n",cvGetFileNodeName(nodeInteger));
		printf("%d\n",cvReadInt(nodeInteger));
	}

	hashNode = cvGetHashedKey(file1,strlen("String"));
	CvFileNode *nodeString = cvGetFileNode(file1,hashNode);
	if (nodeString != NULL){//获取子节点名称
		printf("%s\n",cvGetFileNodeName(nodeString));
		printf("%s\n",cvReadString(nodeString));
	}

	printf("\n");
	printf("%f\n",cvReadRealByName(file1,"Real"));//根据标签获取值
	printf("%s\n",cvReadStringByName(file1,"String"));
	printf("%d\n",cvReadIntByName(file1,"Integer"));


	cvReleaseFileStorage(&file1);
	cvReleaseMemStorage(&mem1);

猜你在找的XML相关文章