开源tinyXMl和OpenCV读写XML文件对比
2016-04-20 21:45
441 查看
<?xml version="1.0" encoding="UTF-8" ?> <Root> <PrivacyData Name="Ӿ˽ʦĜ˽ߝ"> <Face type="ɋs"> <FaceNode> <ID>0</ID> <OperationHistory> <OperationItem> <Polygon>235,152 255,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,152 255,152 255,86 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,152 255,152 255,86 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,152 255,152 255,86 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,"image_%d",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; }
相关文章推荐
- 介绍一款信息管理系统的开源框架---jeecg
- 源码被倒卖,大厂薅羊毛,开源真的只能被予取予求?
- 专家解读:开源软件项目是否会被限制出口?
- 专家解读:开源软件项目是否会被限制出口?
- XML 与 JSON 优劣对比
- fuse-dfs的设定手册
- Centos下***(pptpd)的部署
- As3.0 xml + Loader应用代码
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- 实例解析Ruby程序中调用REXML来解析XML格式数据的用法
- Ruby中XML格式数据处理库REXML的使用方法指南
- C#针对xml基本操作及保存配置文件应用实例
- Ruby使用REXML库来解析xml格式数据的方法
- Ruby程序中创建和解析XML文件的方法
- Ruby的XML格式数据解析库Nokogiri的使用进阶
- asp下查询xml的实现代码
- sqlserver FOR XML PATH 语句的应用