A056313的专栏

现在越来越多的数据和配置采用了xml格式来存放和进行传输解析了。在c++方面,没有本地支持的库,所以需要我们自己去找一下。微软的msxml说实话,确实不咋地,尤其是com的类型变量名字一直指针,让众人看上去就比较反感。开源的tinyxml在这方便做的还不错。简单介绍下使用过程的一点小经验。

在这里发下牢骚,VC6.0以后的各个版本的VS环境对于C++的智能感知都是那么的SB,不管你怎么配置,,怎么google都让你非常抓狂,就是不出来。算了不说了。

从网站上下载tinyxml,下载之后解压打开文件夹,里面有一些测试例子,tinyxml.sln支持vs2010了都,不管这些,怎么需要的是那个xml类库。

使用tinyxml我们只需要

tinyxml.cpp,

tinyxml.h,

tinyxmlerror.cpp,

tinyxmlparser.cpp,

tinystr.cpp,

tinystr.h

6个文件即可。注意一旦少拷贝了其中tinyxmlerror.cpp, tinyxmlparser.cpp, 其中一个或者两个,就会报告link错误,呵呵

在目标源文件的头部,添加 #include"tinyxml.h"和#include "tinystr.h"

我们使用xml文件,无外乎这几种操作,

1. 遍历整个xml返回一棵树select

2. 查找特定节点的属性/文本值select

3.插入特定位置一个节点insert

4.更新特定节点的属性/文本值 update

5.创建xml文件 create

6.who knows

据一位网友的博文里面提到,基本应该涵盖数据库的所有操作,xml操作应该像操作数据库一样。我觉得甚是有道理啊。

那么我来列举下,我搜集以及测试成功的相关的操作代码吧,希望能节省一些大家学习的时间。

1.create xml操作

// 定义一个TiXmlDocument类指针TiXmlDocument *pDoc = new TiXmlDocument;if (NULL==pDoc){return false;}// 定义一个xml文件头部声明TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(("1.0"),(""),(""));if (NULL==pDeclaration){return false;}pDoc->LinkEndChild(pDeclaration);// 生成一个根节点:MyAppTiXmlElement *pRootEle = new TiXmlElement(("MyApp"));if (NULL==pRootEle){return false;}pDoc->LinkEndChild(pRootEle);// 生成子节点:MessagesTiXmlElement *pMsg = new TiXmlElement(("Messages"));if (NULL==pMsg){return false;}pRootEle->LinkEndChild(pMsg);// 生成子节点:WelcomeTiXmlElement *pWelcome = new TiXmlElement(("Welcome"));if (NULL==pWelcome){return false;}pMsg->LinkEndChild(pWelcome);// 设置Welcome节点的值const char* strValue = ("Welcome to MyApp");TiXmlText *pWelcomeValue = new TiXmlText(strValue);pWelcome->LinkEndChild(pWelcomeValue);// 生成子节点:FarewellTiXmlElement *pFarewell = new TiXmlElement(("Farewell"));if (NULL==pFarewell){return false;}pMsg->LinkEndChild(pFarewell);// 设置Farewell节点的值strValue = ("Thank you for using MyApp");TiXmlText *pFarewellValue = new TiXmlText(strValue);pFarewell->LinkEndChild(pFarewellValue);// 生成子节点:WindowsTiXmlElement *pWindows = new TiXmlElement(("Windows"));if (NULL==pWindows){return false;}pRootEle->LinkEndChild(pWindows);// 生成子节点:WindowTiXmlElement *pWindow = new TiXmlElement(("Window"));if (NULL==pWindow){return false;}pWindows->LinkEndChild(pWindow);// 设置节点Window的值pWindow->SetAttribute(("name"),("MainFrame"));pWindow->SetAttribute(("x"),("5"));pWindow->SetAttribute(("y"),("15"));pWindow->SetAttribute(("w"),("400"));pWindow->SetAttribute(("h"),("250"));// 生成子节点:WindowTiXmlElement *pConnection = new TiXmlElement(("Connection"));if (NULL==pConnection){return false;}pRootEle->LinkEndChild(pConnection);// 设置节点Connection的值pConnection->SetAttribute(("ip"),("192.168.0.1"));pConnection->SetAttribute(("timeout"),("123.456000"));pDoc->SaveFile("1.xml");

2.遍历打印xml文件 select操作

//TiXmlDocument *pDoc = new TiXmlDocument();if (NULL==pDoc){return false;}pDoc->LoadFile("1.xml");pDoc->Print();

3.获取单个节点值

bool GetNodePointerByName(TiXmlElement* pRootEle,std::string &strNodeName,TiXmlElement* &Node) {// 假如等于根节点名,就退出if (strNodeName==pRootEle->Value()){Node = pRootEle;return true;}TiXmlElement* pEle = pRootEle;for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement()){//递归处理子节点,获取节点指针if(GetNodePointerByName(pEle,strNodeName,Node))return true;}return false; } bool QueryNode_Text(std::string XmlFile,std::string strNodeName,std::string &strText) {// 定义一个TiXmlDocument类指针TiXmlDocument *pDoc = new TiXmlDocument();if (NULL==pDoc){return false;}pDoc->LoadFile(XmlFile);TiXmlElement *pRootEle = pDoc->RootElement();if (NULL==pRootEle){return false;}TiXmlElement *pNode = NULL;GetNodePointerByName(pRootEle,strNodeName,pNode);if (NULL!=pNode){strText = pNode->GetText();return true;}else{return false;}} bool QueryNode_Attribute(std::string XmlFile,std::string strNodeName,std::map<std::string,std::string> &AttMap) {// 定义一个TiXmlDocument类指针typedef std::pair <std::string,std::string> String_Pair;TiXmlDocument *pDoc = new TiXmlDocument();if (NULL==pDoc){return false;}pDoc->LoadFile(XmlFile);TiXmlElement *pRootEle = pDoc->RootElement();if (NULL==pRootEle){return false;}TiXmlElement *pNode = NULL;GetNodePointerByName(pRootEle,strNodeName,pNode);if (NULL!=pNode){TiXmlAttribute* pAttr = NULL;for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next()){std::string strAttName = pAttr->Name();std::string strAttValue = pAttr->Value();AttMap.insert(String_Pair(strAttName,strAttValue));}return true;}else{return false;}return true; } 4.删除节点操作

生活中若没有朋友,就像生活中没有阳光一样

A056313的专栏

相关文章:

你感兴趣的文章:

标签云: