我怎样才能在c ++中使用xml?

时间:2015-03-18 10:11:52

标签: c++ xml opencv

我从c ++开始,我尝试在我的代码中使用xml,这是我的源代码:

    CvFileStorage * fileStorage;
    fileStorage = cvOpenFileStorage( "facedata.xml", 0, CV_STORAGE_WRITE );                            
    string d="apple";
    char* s=new char();
    strcpy(s,d.c_str());
    cvWrite(fileStorage, "word", s);
    cvReleaseFileStorage( &fileStorage );
    fileStorage = cvOpenFileStorage( "facedata.xml", 0, CV_STORAGE_READ );
    s=cvReadStringByName(fileStorage, 0, "word",0);
    cout<<s<<endl;

s未返回word的内容,我需要您的帮助。

1 个答案:

答案 0 :(得分:1)

再次,请使用opencv的c ++ api:

cv::FileStorage fs("facedata.xml", cv::FileStorage::WRITE);
fs << "word" << "apple";
fs.release();

cv::FileStorage fs2("facedata.xml", cv::FileStorage::READ);
string s;
fs2["word"] >> s;
fs2.release();