vs2012下tinyXml解析xml

来源:互联网 发布:邓丽君并没有网络传 编辑:程序博客网 时间:2024/05/01 21:22

TinyXML是一开源解析XML的库,其原理通过解析XML文件,然后在内存中生成DOM模型,从而方便的便利XML树。

一、在官网下载源码,通过编译生成两个静态链接库分别为:tinyxmlSTL.lib和tinyxml.lib。

二、在vs2012新建一工程,在其目录下包含上述lib和头文件。

三、如果有链接错误,在项目属性-》c++-》代码生成下运行库中改为多线程调试(/MTD)。注:项目在debug下。


类定义如下:

TiXmlBase:整个TinyXML模型的基类。

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

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

TiXmlComment:对应于XML中的注释

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

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

TiXmlElement:对应于XML的元素。

TiXmlText:对应于XML的文字部分

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

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


入门教程查看链接:http://www.cnblogs.com/kex1n/archive/2010/10/03/1841502.html


读入一app执行文件目录

CString GetAppPath(){TCHAR modulePath[MAX_PATH];GetModuleFileName(NULL, modulePath, MAX_PATH);CString strModulePath(modulePath);strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));return strModulePath;}

---------------------------------------------------------------------------------------------------------------------

在编译过程中和实际工程中各种问题,转战tinyXML2

tinyXML2官网:http://www.grinninglizard.com/tinyxml2/index.html

下载链接:https://github.com/leethomason/tinyxml2

下载zip解压后,在我们实际工程中只需tinyxml2.h与tinyxml2.cpp文件,分别导入项目即可。不过在编译时在tinyxml2.cpp文件中加入预编译#include“stdafx.h”。

可以参考这篇博文:

http://www.tuicool.com/articles/uYBB7j

以下是具体实例:

// ConsoleApplication5.cpp : ¶¨Òå¿ØÖÆ̨ӦÓóÌÐòµÄÈë¿Úµã¡£//#include "stdafx.h"#include <iostream>#include "tinyxml2.h"#include <string>using namespace std;using namespace tinyxml2;//xml文件/*   <?xml version="1.0"?><scene name="Depth"><node type="camera"><eye>0 10 10</eye><front>0 0 -1</front><refUp>0 1 0</refUp><fov>90</fov></node><node type="Sphere"><center>0 10 -10</center><radius>10</radius></node><node type="Plane"><direction>0 10 -10</direction><distance>10</distance></node></scene>*///遍历整个xml文件void traversalXML(const char* pPath){XMLDocument doc;doc.LoadFile(pPath);XMLElement *scene=doc.RootElement();XMLElement *surface=scene->FirstChildElement("node");while (surface){XMLElement *surfaceChild=surface->FirstChildElement();const char* content;const XMLAttribute *attributeOfSurface = surface->FirstAttribute();cout<< attributeOfSurface->Name() << ":" << attributeOfSurface->Value() << endl;while(surfaceChild){content=surfaceChild->GetText();surfaceChild=surfaceChild->NextSiblingElement();cout<<content<<endl;}surface=surface->NextSiblingElement();}}//取第二组node输出void getSNode(const char* pPath, const char* strType)   //pPath代表xml文件路径{XMLDocument doc;doc.LoadFile(pPath);XMLElement *scene = doc.RootElement();XMLElement *surface = scene->FirstChildElement("node");const char* content;while (surface){const XMLAttribute *attributeSurface = surface->FirstAttribute();//strcmp比较两个字符串大小,如果相等返回为0,前者大返回1,后者大返回-1if (!strcmp(attributeSurface->Value(),strType)){XMLElement *surfaceChild = surface->FirstChildElement();while(surfaceChild){content = surfaceChild->GetText();surfaceChild = surfaceChild->NextSiblingElement();cout << content << endl;}}surface = surface->NextSiblingElement();}}int _tmain(int argc, _TCHAR* argv[]){/*traversalXML("test.xml");*/getSNode("test.xml", "Sphere");return 0;}


0 0