加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

利用TinyXML对mxl文件的读写

发布时间:2020-12-15 22:44:02 所属栏目:百科 来源:网络整理
导读:TinyXML 是一个开源的解析 XML 的解析库,能够用于 C++ ,能够在 Windows 或 Linux 中编译。这个解析库的模型通过解析 XML 文件,然后在内存中生成 DOM 模型,从而让我们很方便的遍历这棵 XML 树。 DOM 模型即文档对象模型,是将整个文档分成多个元素(如书

TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在WindowsLinux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。

DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。

如下是一个XML片段:

   <Persons>
        <Person ID="1">
            <name>李明</name>
            <age>20</age>
        </Person>
        <Person ID="2">
            <name>张三</name>
            <age>18</age>
        </Person>
    </Persons>

TinyXML中,根据XML的各种元素来定义了一些类:

TiXmlBase:整个TinyXML模型的基类。

TiXmlAttribute:对应于XML中的元素的属性。

TiXmlNode:对应于DOM结构中的节点。

TiXmlComment:对应于XML中的注释

TiXmlDeclaration:对应于XML中的申明部分,即<versiong="1.0"?>

TiXmlDocument:对应于XML的整个文档。

TiXmlElement:对应于XML的元素。

TiXmlText:对应于XML的文字部分

TiXmlUnknown:对应于XML的未知部分。

TiXmlHandler:定义了针对XML的一些操作。

TinyXML

TinyXML是个解析库,主要由DOM模型类(TiXmlBaseTiXmlNodeTiXmlAttributeTiXmlCommentTiXmlDeclarationTiXmlElementTiXmlTextTiXmlUnknown)和操作类(TiXmlHandler)构成。

它由两个头文件(.h文件)和四个CPP文件(.cpp文件)构成,用的时候,只要将(tinyxml.htinystr.htinystr.cpptinyxml.cpptinyxmlerror.cpptinyxmlparser.cpp)导入工程就可以用它的东西了。如果需要,可以将它做成自己的DLL来调用。举个例子就可以说明一切。。。

对应的XML文件:

<Persons>
    <Person ID="1">
        <name>phinecos</name>
        <age>22</age>
    </Person>
</Persons>

读写XML文件操作。新建一个Win32工程,命名为:readandwriteXML。在当前目录中添加文件夹文件夹TiXML,文件夹包含如下内容:

工程目录如下图所示:

整地代码如下:

完//readandwriteXML.cpp:定义控制台应用程序的入口点。

#include "stdafx.h"
#include <iostream>
#include "TiXML/tinyxml.h"
#include "TiXML/tinystr.h"
#include <string>
#include <windows.h>
#include <atlstr.h>
using namespace std;
 
string GetAppPath()
{
//获取应用程序根目录    
TCHAR modulePath[MAX_PATH];    
GetModuleFileName(NULL,modulePath,MAX_PATH);    
CString strModulePath(modulePath);    
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('')));  
std::string strstring = CT2A(strModulePath.GetBuffer());
return strstring;
}
bool CreateXmlFile(string& szFileName)
{
//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false    
try    
{        
//创建一个XML的文档对象。        
TiXmlDocument *myDocument = new TiXmlDocument();        
//创建一个根元素并连接。        
TiXmlElement *RootElement = new TiXmlElement("Persons");       
myDocument->LinkEndChild(RootElement);        
//创建一个Person元素并连接。        
TiXmlElement *PersonElement = new TiXmlElement("Person");        
RootElement->LinkEndChild(PersonElement);        
//设置Person元素的属性。        
PersonElement->SetAttribute("ID","1");        
//创建name元素、age元素并连接。        
TiXmlElement *NameElement = new TiXmlElement("name");        
TiXmlElement *AgeElement = new TiXmlElement("age");        
PersonElement->LinkEndChild(NameElement);        
PersonElement->LinkEndChild(AgeElement);        
//设置name元素和age元素的内容并连接。        
TiXmlText *NameContent = new TiXmlText("李明");        
TiXmlText *AgeContent = new TiXmlText("22");        
NameElement->LinkEndChild(NameContent);       
AgeElement->LinkEndChild(AgeContent);        
string appPath = GetAppPath();
string seperator = "";
string fullPath = appPath + seperator + szFileName;
myDocument->SaveFile(fullPath.c_str());
//保存到文件    
}    
catch (string& e)    
{        
return false;    
}    
return true;
}
bool ReadXmlFile(string& szFileName)
{
//读取Xml文件,并遍历    
try    
{
string appPath = GetAppPath();
string seperator = "";
string fullPath = appPath + seperator + szFileName;
//创建一个XML的文档对象。        
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());        
myDocument->LoadFile();        
//获得根元素,即Persons。        
TiXmlElement *RootElement = myDocument->RootElement();        
//输出根元素名称,即输出Persons。        
cout << RootElement->Value() << endl;        
//获得第一个Person节点。        
TiXmlElement *FirstPerson = RootElement->FirstChildElement();        
//获得第一个Person的name节点和age节点和ID属性。        
TiXmlElement *NameElement = FirstPerson->FirstChildElement();        
TiXmlElement *AgeElement = NameElement->NextSiblingElement();        
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();        
//输出第一个Person的name内容,即李明;age内容,即;ID属性,即。        
cout << NameElement->FirstChild()->Value() << endl;        
cout << AgeElement->FirstChild()->Value() << endl;        
cout << IDAttribute->Value()<< endl;    }    
catch (string& e)    
{        
return false;    
}    
return true;
}
 
int _tmain(int argc,_TCHAR* argv[])
{
string fileName = "info.xml";    
CreateXmlFile(fileName);    
ReadXmlFile(fileName);
system("pause");
return 0;
}

运行结果:

注:TinyXML下载地址:http://download.csdn.net/detail/shufac/7422783

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读