<?xml version="1.0" encoding="UTF-8" ?>
<Root>
<PrivacyData Name="Ӿ˽ʦĜ˽ߝ">
<Face type="ɋs">
<FaceNode>
<ID>0</ID>
<OperationHistory>
<OperationItem>
<polygon>235,152 255,86 235,86</polygon>
<OperationFlag>ԌѲʺԉ</OperationFlag>
<Operator>(ާĦˤɫܲ֟τݾƤ׃)</Operator>
<Time>2013.10.12 17:10:08</Time>
</OperationItem>
</OperationHistory>
</FaceNode>
<FaceNode>
<ID>1</ID>
<OperationHistory>
<OperationItem>
<polygon>235,86</polygon>
<OperationFlag>ԌѲʺԉ</OperationFlag>
<Operator>(ާĦˤɫܲ֟τݾƤ׃)</Operator>
<Time>2013.10.12 17:10:08</Time>
</OperationItem>
</OperationHistory>
</FaceNode>
<FaceNode>
<ID>2</ID>
<OperationHistory>
<OperationItem>
<polygon>235,86</polygon>
<OperationFlag>ԌѲʺԉ</OperationFlag>
<Operator>(ާĦˤɫܲ֟τݾƤ׃)</Operator>
<Time>2013.10.12 17:10:08</Time>
</OperationItem>
</OperationHistory>
</FaceNode>
</Face>
<Secrety type="ʦĜ" />
</PrivacyData>
</Root>
采用tinyXML读写上述文件:
#include <fstream>
#include <iostream>
#include <sstream>
#include <windows.h>
#include <string>
#include <stdio.h>
#include "stdlib.h"
#include "direct.h"
#include "string.h"
#include "io.h"
using namespace std;
#include "tchar.h"
#include "tinyxml.h"
/*! * /brief 创建XML文件。 * * /param XmlFile xml文件全路径。 * /return 是否成功。true为成功,false表示失败。 */
bool CreateXml(char * XmlFile)
{
// 定义一个tixmlDocument类指针
tixmlDocument *pDoc = new tixmlDocument;
if (NULL==pDoc)
{
return false;
}
tixmlDeclaration *pDeclaration = new tixmlDeclaration(_T("1.0"),_T("UTF-8"),_T(""));
if (NULL==pDeclaration)
{
return false;
}
pDoc->LinkEndChild(pDeclaration);
// 生成一个根节点:Root
tixmlElement *pRootEle = new tixmlElement(_T("Root"));
if (NULL==pRootEle)
{
return false;
}
pDoc->LinkEndChild(pRootEle);
// 生成子节点:Messages
tixmlElement *pMsg = new tixmlElement(_T("PrivacyData"));
if (NULL==pMsg)
{
return false;
}
pRootEle->LinkEndChild(pMsg);
pMsg->SetAttribute(_T("Name"),_T("隐私涉密数据"));
// 生成子节点:Face
tixmlElement *pFace = new tixmlElement(_T("Face"));
pMsg->LinkEndChild(pFace);
pFace->SetAttribute(_T("type"),_T("人脸"));
for(int id = 0;id<3;id++){
// 生成子节点:FaceNode
tixmlElement *pFaceNode = new tixmlElement(_T("FaceNode"));
pFace->LinkEndChild(pFaceNode);
// 生成子节点:TD
tixmlElement *pId = new tixmlElement(_T("ID"));
pFaceNode->LinkEndChild(pId);
// 设置ID节点的值
char strValue[1000] ;
sprintf(strValue,"%d",id);
tixmlText *pWelcomeValue = new tixmlText(strValue);
pId->LinkEndChild(pWelcomeValue);
// 生成子节点:OperationHistory
tixmlElement *pOperationHistory = new tixmlElement(_T("OperationHistory"));
pFaceNode->LinkEndChild(pOperationHistory);
// 生成子节点:OperationItem
tixmlElement *pOperationItem = new tixmlElement(_T("OperationItem"));
pOperationHistory->LinkEndChild(pOperationItem);
// 生成子节点:polygon // 设置polygon节点的值
tixmlElement *ppolygon = new tixmlElement(_T("polygon"));
pOperationItem->LinkEndChild(ppolygon);
char * polygonValue = _T("235,86");
tixmlText *ppolygonValue = new tixmlText(polygonValue);
ppolygon->LinkEndChild(ppolygonValue);
// 生成子节点:OperationFlag // 设置OperationFlag节点的值
tixmlElement *pOperationFlag = new tixmlElement(_T("OperationFlag"));
pOperationItem->LinkEndChild(pOperationFlag);
char * operationFlagValue = _T("程序生成");
tixmlText *pOperationFlagValue = new tixmlText(operationFlagValue);
pOperationFlag->LinkEndChild(pOperationFlagValue);
// 生成子节点:OperationFlag // 设置OperationFlag节点的值
tixmlElement *pOperator = new tixmlElement(_T("Operator"));
pOperationItem->LinkEndChild(pOperator);
char * oOperatorValue = _T("(界面输入或者文件配置)");
tixmlText *pOperatorValue = new tixmlText(oOperatorValue);
pOperator->LinkEndChild(pOperatorValue);
// 生成子节点:OperationFlag // 设置OperationFlag节点的值
tixmlElement *pTime = new tixmlElement(_T("Time"));
pOperationItem->LinkEndChild(pTime);
char * oTimeValue = _T("2013.10.12 17:10:08");
tixmlText *pTimeValue = new tixmlText(oTimeValue);
pTime->LinkEndChild(pTimeValue);
}
// 生成子节点:Secrety
tixmlElement *pSecrety = new tixmlElement(_T("Secrety"));
pMsg->LinkEndChild(pSecrety);
pSecrety->SetAttribute(_T("type"),_T("涉密"));
pDoc->SaveFile(XmlFile);
return true;
}
void main(){
CreateXml("test.xml");
}
插一个用OpenCV读写的简单例子
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
class foo{
public:
vector<string> imnames;
void write(FileStorage &fs) const;
void read(const FileNode& node);
};
void foo::write(FileStorage &fs) const
{
assert(fs.isOpened());
fs << "{";
fs << "nimages" << (int)imnames.size();
for(int i = 0; i < int(imnames.size()); i++)
{
char str[256];
const char* ss;
sprintf(str,"image_%d",i);
ss = str;
fs << ss << imnames[i];
}
fs << "}";
}
void foo::read(const FileNode& node)
{
assert(node.type() == FileNode::MAP);
int n;
node["nimages"] >> n;
imnames.resize(n);
for(int i = 0; i < n; i++){
char str[256];
const char* ss;
sprintf(str,i);
ss = str;
node[ss] >> imnames[i];
}
}
template <class T> T load_ft(const char* fname){
T x;
FileStorage f(fname,FileStorage::READ);
f["ftobject"] >> x; f.release(); return x;
}
template<class T> void save_ft(const char* fname,const T& x){
FileStorage f(fname,FileStorage::WRITE);
f << "ftobject" << x; f.release();
}
template<class T> void write(FileStorage& fs,const string&,const T& x)
{
x.write(fs);
}
template<class T> void read(const FileNode& node,T& x,const T& d)
{
if(node.empty())x = d; else x.read(node);
}
int main(int argc,const char *argv[])
{
//构造数据
foo A;
string name1="xhl";
string name2="gyl";
A.imnames.push_back(name1);
A.imnames.push_back(name2);
//存储A对象到foo.xml文件
save_ft<foo>("foo.xml",A);
foo B;
//从foo.xml文件读入,返回的临时对象赋给b
B=load_ft<foo>("foo.xml");
//测试
cout<<B.imnames[0]<<endl;
cout<<B.imnames[1]<<endl;
}