Chinaunix首页 | 论坛 | 博客
  • 博客访问: 293160
  • 博文数量: 81
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 952
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-31 22:05
文章分类

全部博文(81)

文章存档

2011年(1)

2010年(1)

2009年(79)

我的朋友

分类: 系统运维

2009-08-17 14:13:37

TinyXml是一个基于DOM模型的、非验证的轻量级C++解释器


一. XML解析模型:

目前XML的解析主要有两大模型:SAX和DOM。

SAX是基于事件的,其基本工作流程是分析XML文档,当发现了一个新的元素时,产生一个对应事件,并调用相应的用户处理函数。这种方式占用内存少,速度快,但用户程序相应得会比较复杂。

DOM(文档对象模型),则是在分析时,一次性的将整个XML文档进行分析,并在内存中形成对应的树结构,同时,向用户提供一系列的接口来访问和编辑该树结构。这种方式占用内存大,速度往往慢于SAX,但可以给用户提供一个面向对象的访问接口,对用户更为友好。

另据说,一些同时提供了SAX和DOM接口的库,是在底层先实现SAX,再在SAX的基础上实现DOM



对于一个特定的XML文档而言,其正确性分为两个层次。

首先是其格式应该符合XML的基本格式要求,比如第一行要有声明,标签的嵌套层次必须前后一致等等,符合这些要求的文件,就是一个合格的XML文件,称作well-formatted。

其次,一个XML文档因其内容的不同还必须在语义上符合相应的标准,这些标准由相应的DTD文件或者Schema文件来定义,符合了这些定义要求的XML文件,称作valid。

因此,解析器也分为两种,一种是验证的,即会跟据XML文件中的声明,用相应的DTD文件对XML文件进行校验,检查它是否满足DTD文件的要求。另一种是忽略DTD文件,只要基本格式正确,就可以进行解析。

就我所知,验证的解析器通常都是比较重量级的。TinyXml不支持验证,但是体积很小,用在解析格式较为简单的XML文件,比如配置文件时,特别的合适。

二. TinyXml 介绍:


Tiny Xml Online Documentation

TinyXml Class Hierarchy
This inheritance list is sorted roughly, but not completely, alphabetically:
TiXmlBase
TiXmlAttribute
TiXmlNode
TiXmlComment
TiXmlDeclaration
TiXmlDocument
TiXmlElement
TiXmlText
TiXmlUnknown
TiXmlHandle
TiXmlVisitor
TiXmlPrinter



TinyXml实现的时DOM访问模型,因此提供了一系列的类对应XML文件中的各个节点。主要类间的关系如下图所示:




TiXmlBase:其他类的基类,是个抽象类

TiXmlNode:表示一个节点,包含节点的一般方法,如访问自节点、兄弟节点、编辑自身、编辑子节电

TiXmlDocument:表示整个XML文档,不对应其中某个特定的节点。

TiXmlElement:表示元素节点,可以包含子节点和TiXmlAttribute

TiXmlComment:表示注释

TiXmlDeclaration:表示声明

TiXmlText:表示文本节点

TiXmlUnknown:表示未知节点,通常是出错了

TiXmlAttribute:表示一个元素的属性



各类之间的转换

由于各个节点类都从TiXmlNode继承,在使用时常常需要将TiXmlNode*类型的指针转换为其派生类的指针,在进行这种转换时,应该首先使用由 TiXmlNode类提供的一系列转换函数,如ToElement(void),而不是c++的dynamic_cast


检查返回值

由于TinyXml是一个非校验的解析器,因此当解析一个文件时,很可能文件并不包含我们预期的某个节点,在这种情况下,TinyXml将返回空指针。因此,必须要对返回值进行检查,否则将很容易出现内存访问的错误。




如何重头建立一个XML文件

先建立一个TiXmlDocument对象,然后,载入某个模板,或者直接插入一个节点作为根节点,接着就可以像打开一个已有的XML文件那样对它进行操作了。



三. TinyXml 实践:Linux: Makefile setting

# DEBUG can be set to YES to include debugging info, or NO otherwise
DEBUG := YES

# PROFILE can be set to YES to include profiling info, or NO otherwise
PROFILE := NO

# TINYXML_USE_STL can be used to turn on STL support. NO, then STL
# will not be used. YES will include the STL files.
TINYXML_USE_STL := YES


TinyXml在构建时可以选择是否支持STL,选择的话,则可以使用std::string,所以通常应该打开这个选项。

在Windows上,TinyXml的源码包里提供了VC6的工程文件,直接用它就可以生成两个静态库(带STL和不带STL),非常容易。唯一需要注意的是,默认生成的库是单线程的,如果用在多线程的项目中,需要改动一下配置,生成相应的多线程库。

构建了相应的库之后,在使用了它们的工程中,只要在连接时把他们连上就行了。需要注意的是,如果需要STL支持,在编译用到了TinyXml的文件时,需 要定义一个宏TIXML_USE_STL,对gcc,可以使用参数-DTIXML_USE_STL,对cl.exe(VC),可以使用参数 /DTIXML_USE_STL,如果嫌麻烦,可以直接定义在 tinyxml.h文件里。

附件: pro


四. 例程:

引自: http://www.cnblogs.com/phinecos/archive/2008/03/11/1100912.html




#include
#include "tinyxml.h"
#include "tinystr.h"
#include
#include
#include
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);
}
阅读(3197) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~