开源tinyXMl和OpenCV读写XML文件对比

前端之家收集整理的这篇文章主要介绍了开源tinyXMl和OpenCV读写XML文件对比前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<?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;

}
原文链接:https://www.f2er.com/xml/295321.html

猜你在找的XML相关文章