C++中操作XML文件开源库-TinyXml

来源:互联网 发布:抽烟知乎 编辑:程序博客网 时间:2024/04/27 22:07

源代码地址:http://sourceforge.net/projects/tinyxml/

我的资源的地址  http://download.csdn.net/detail/duhaomin/7439485

操作非常的便利,将我的资源下载后,编译一下生成一个静态库

然后在想要使用该开源库的项目中将生成的lib库的路径添加进来,然后在你的项目中将tinystr.h和tinyxml.h添加,这样就可以使用了,使用的非常简单的例子:


创建XML文件对象(其实是链表做的存储链表):

TiXmlDocument *pDoc = new TiXmlDocument;  if (NULL==pDoc)  {  return false;  }  


然后是声明XML的文件版本、字符集等:

TiXmlDeclaration *pDeclaration = new TiXmlDeclaration("1.0","gb2312","");  if (NULL==pDeclaration)  {  return false;  }  

因为XML第一个信息是声明,所以现在直接将声明添加到总的XML链表中:

pDoc->LinkEndChild(pDeclaration);  

之后添根节点:

// 生成一个根节点TiXmlElement *pRootEle = new TiXmlElement("根节点");  pDoc->LinkEndChild(pRootEle);

然后就是最简单的一个个的节点:

TiXmlElement *pHeader = new TiXmlElement("第一个节点");  pRootEle->LinkEndChild(pHeader); 


现在我们生成一个最简单的XML文件

bool testxml(){TiXmlDocument *pDoc = new TiXmlDocument;  if (NULL==pDoc)  {  return false;  }  TiXmlDeclaration *pDeclaration = new TiXmlDeclaration("1.0","gb2312","");  if (NULL==pDeclaration)  {  return false;  }  pDoc->LinkEndChild(pDeclaration);  // 生成一个根节点TiXmlElement *pRootEle = new TiXmlElement("根节点");  pDoc->LinkEndChild(pRootEle);  //头节点TiXmlElement *pHeader = new TiXmlElement("第一个节点");  pRootEle->LinkEndChild(pHeader);  TiXmlElement *pNode = new TiXmlElement("str1");  pHeader->LinkEndChild(pNode);  TiXmlText *pValue = new TiXmlText("1");  pNode->LinkEndChild(pValue);pNode = new TiXmlElement("str2");  pHeader->LinkEndChild(pNode);  pNode->SetAttribute("Timey","2014");pNode->SetAttribute("Timem","06");char out[MAX_PATH]={0};strcpy(out,"D:");strcat(out,"\\");strcat(out,"testxml.xml");pDoc->SaveFile(out);  return true;  }


效果:


是不是很简单呢?









0 0
原创粉丝点击