TinyXML:一个优秀的C++ XML解析器
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。
TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。
如下是一个XML片段:
<Person ID="1">
<name>周星星</name>
<age>20</age>
</Person>
<Person ID="2">
<name>白晶晶</name>
<age>18</age>
</Person>
</Persons>
首先类结构图:
上图错了,TiXmlElement 与TiXmlAttributeSet 是关联关系,而非依赖.
TiXmlAttributeSet 与 TiXmlAttribute既关联 又依赖
在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是个解析库,主要由DOM模型类(TiXmlBase、TiXmlNode、TiXmlAttribute、TiXmlComment、TiXmlDeclaration、TiXmlElement、TiXmlText、TiXmlUnknown)和操作类(TiXmlHandler)构成。它由两个头文件(.h文件)和四个CPP文件(.cpp文件)构成,用的时候,只要将(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)导入工程就可以用它的东西了。如果需要,可以将它做成自己的DLL来调用。举个例子就可以说明一切。。。
对应的XML文件:
<Person ID="1">
<name>phinecos</name>
<age>22</age>
</Person>
</Persons>
读写XML文件的程序代码:
#include <iostream>
#include "tinyxml.h"
#include "tinystr.h"
#include <string>
#include <windows.h>
#include <atlstr.h>
using namespace std;
CString GetAppPath()
{//获取应用程序根目录
TCHAR modulePath[MAX_PATH];
GetModuleFileName(NULL, modulePath, MAX_PATH);
CString strModulePath(modulePath);
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
return strModulePath;
}
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);
CString appPath = GetAppPath();
string seperator = "\\";
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
myDocument->SaveFile(fullPath.c_str());//保存到文件
}
catch (string& e)
{
return false;
}
return true;
}
bool ReadXmlFile(string& szFileName)
{//读取Xml文件,并遍历
try
{
CString appPath = GetAppPath();
string seperator = "\\";
string fullPath = appPath.GetBuffer(0) +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 main()
{
string fileName = "info.xml";
CreateXmlFile(fileName);
ReadXmlFile(fileName);
}
附: 生成xml要领:
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration("1.0","UTF-8","yes" );
TiXmlElement * element = new TiXmlElement( "Hello" );
TiXmlText * text = new TiXmlText( "World" );
element->LinkEndChild( text );
doc.LinkEndChild( decl );
doc.LinkEndChild( element );
doc.SaveFile( "madeByHand.xml" );
转载于:https://www.cnblogs.com/weiqubo/archive/2011/01/14/1935854.html
TinyXML:一个优秀的C++ XML解析器相关推荐
- Phinecos(洞庭散人) 专注于开源技术的研究与应用 TinyXML:一个优秀的C++ XML解析器
读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好. TinyXML是一个开源的解 ...
- java xml解析器_Java XML解析器
java xml解析器 Java XML parser is used to work with xml data. XML is widely used technology to transpor ...
- Java XML解析器
使用Apache Xerces解析XML文档 一.技术概述 在用Java解析XML时候,一般都使用现成XML解析器来完成,自己编码解析是一件很棘手的问题,对程序员要求很高,一般也没有专业厂商或者开源组 ...
- XML 解析器之一 :MSXML使用教程(转)
介绍 微软的msxml是基于COM接口开发的,如同vbscript和javascript一样,微软这么做是为了提供更好的扩展性.你可以在用脚本来调用msxml,也可以用C++这样编程语言一样调用(虽然 ...
- DOM4J——强大而易用的XML解析器,我们不只是有数据库
DOM4J--强大而易用的XML解析器 DOM4J 一.DOM4J的导入使用 二.认识一些关键角色 三.快速开始 3.1创建一个XML 3.2读取XML节点(历遍和指定) 3.3规范输出 结尾 欢迎关 ...
- 先弄个XML解析器代码抄一抄 慢慢研究 O(∩_∩)O哈哈~
出处:http://bbs.csdn.net/topics/390229172 已经自我放逐好几年了.打算去上班得了.在最后的自由日子里,做点有意义的事吧... 先来下载地址 http:/ ...
- Android XML解析器– XMLPullParser
Welcome to android xml parser example using XMLPullParser. We will have a sample XML file that we wi ...
- Python XML解析器– ElementTree
Python XML parser provides us an easy way to read the XML file and extract useful data. Today we wil ...
- XML解析器及相关概念介绍
前几天看一本介绍JSP的书,上面有对XML解析器的介绍,但看不太懂,在网上搜了一些资料,看后发现原来书中写的不太正确. 通过这篇文章,把本人理解的关于XML解析器和Java下一些XML相关的概念介绍清 ...
最新文章
- usaco The Perfect Stall(二分匹配模板)
- how tomcat works 总结 二
- WindowType 属性
- 基于注释的Spring Security实战
- 从开源自治,到微服务云化,阿里云的这款产品给了一剂提升微服务幸福感的良药
- java值栈_Struts2学习笔记-Value Stack(值栈)和OGNL表达式
- c# 审批流引擎_小熊OA:流程引擎才能真正起到管理价值!
- python中画圆的代码_Python使用matplotlib绘制圆形代码实例
- 大数据分析有什么难题
- mysql id 主键 外键_mysql主键 外键
- 【图像修复】论文阅读笔记 ----- 《Image inpainting based on deep learning: A review》
- 多商户商城系统功能拆解23讲-平台端分销等级
- opencv不规则掩膜裁剪图片
- 仿elem页面学习之表单提交的动作
- sqlserver官方网站地址
- Relief与Relie-F
- 目标检测算法——YOLOv5/YOLOv7改进之结合特征提取网络RFBNet(涨点明显)
- 高考之后,泪眼婆娑,爱很脆弱
- 计算机二级电子商务考试内容,电子商务师考试内容
- 各大语言之父,你认识几个?Python之父,头发最茂盛?