Some simple XML read/write operations are recorded in notes for memo
Main functions:
1. Create XML
2. Store or read basic Int float data in XML
3. Create XML elements to access complex structures such as struct and matrix.
The Code is as follows:
// BasicExample. cpp: Defines the entry point for the console application. // # include "stdafx. h "# include <iostream> # include" opencv2/opencv. hpp "using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {// create an XML file storage CvFileStorage * fs = cvOpenFileStorage (" jarvischu. xml ", 0, // memory used for storage. If it is NULL, a temporary memory CV_STORAGE_WRITE," GB2312 "// encoded) is used; // write data cvWriteInt (fs, "frame_count", 10); // -- write IntcvWriteReal (fs, "pi", 3.14); // -- write Float // -- write struct cvStartWriteStruct (fs, "frame_size ", CV_NODE_MAP, "id_size"); // There is a name (a new tag will be created) cvWriteInt (fs, "width", 320); cvWriteInt (fs, "height", 200 ); cvEndWriteStruct (fs); cvStartWriteStruct (fs, "author_info", CV_NODE_SEQ, "id_author"); // no name for cvWriteString (fs, 0, "JarvisChu"); cvWriteString (fs, 0, "China"); cvEndWriteStruct (fs); // -- write matrix unsigned char vec [] = {1, 2, 3, 4, 5, 6, 7, 8, 9 }; cvMat mat = cvMat (3, 3, CV_8UC1, vec); cvWrite (fs, "Matrix", & mat); // -- write comments cvWriteComment (fs, "This is a example for operatoring the xml file", 0); // if it is not 0, it indicates that it is placed in the current row, and 0 indicates a new row. // release cvReleaseFileStorage (& fs ); // open the XML file fs = cvOpenFileStorage ("jarvischu. xml ", 0, // memory used for storage. If it is NULL, a temporary memory CV_STORAGE_READ," GB2312 "// encoded ); // read data int f = cvReadIntByName (fs, 0, "frame_count", 0 ); // read the Int with a name // -- read the element data CvFileNode * fn = cvGetFileNodeByName (fs, 0, "frame_size"); // read the parent element (TAG, node) first) int width = cvReadIntByName (fs, fn, "width"); // read the child element int height = cvReadIntByName (fs, fn, "height") through the parent element "); // -- read the ordered data in the element fn = cvGetFileNodeByName (fs, 0, "author_info"); // read the element CvSeq * seq = fn-> data first. seq; // obtain the Sequential stream of elements. const char * name = cvReadString (CvFileNode *) cvGetSeqElem (seq, 0 )); // read the data in the Sequential stream 0 const char * country = cvReadString (CvFileNode *) cvGetSeqElem (seq, 1 )); // read the data 1 cout in the ordered stream <"frame_count:" <f <endl <"width: "<width <endl <" height "
The running result is as follows:
The jarvischu. xml file is as follows: