下载TinyXML库文件http://sourceforge.net/projects/tinyxml/

在TinyXML的目录找到tinystr.h, tinyxml.h,tinystr.cpp,tinyxml.cpp, tinyxmlerror.cpp,tinyxmlparser.cpp六个文件加入到自己的项目中去,在相应的工程文件中加入两个头文件 #include "tinyxml.h"   ,#include "tinystr.h",在 tinystr.cpp,tinyxml.cpp, tinyxmlerror.cpp, tinyxmlparser.cpp四个文件的第一行加入头文件 #include "stdafx.h",然后即可使用TinyXML编程。

=======================================================

要读取的xml 数据如下:

<?xml version="1.0" encoding="gb2312" standalone="yes" ?>

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<prop>

<id>100</id>

<title> test 1</title>

</prop>

<prop>

<id>200</id>

<title> test 2</title>

</prop>

</root>

注意要将 encoding设为gb2312格式,我一开始设置的是utf-8,结果遇到在程序里

写入中文没问题, 但在读出该中文时却有异常,把后面的 </ 符号也当作值读出来

了,后来和一同事讨论后才知道是编码问题。

=======================================================

string   filefullPath = 要读取xml文件的绝对路径

//创建文件对象

TiXmlDocument *  myDocument = new TiXmlDocument(filefullPath.c_str());

//加载文件数据

myDocument->LoadFile();

//获取根节点

TiXmlElement *RootElement = myDocument->RootElement();

=======================================================

以下是简单的读取操作:

//第一个子节点     TiXmlElement *CurrentPerson = RootElement->FirstChildElement();

//遍历获取指定节点数据      while(CurrentPerson)     {

//子节点第一个属性  id          TiXmlElement *IdElement = CurrentPerson->FirstChildElement();

//第一个属性的值          int  nodeID = atoi(IdElement->FirstChild()->Value());

//子节点第二个属性  title          TiXmlElement *TitleElement = IdElement->NextSiblingElement();

//第二个属性的值          CString   nodeTitle = TitleElement->FirstChild()->Value();

.....................

如果还有后续属性,依次读取

.....................

维护读出的数据

.....................

//指向下一节点          CurrentPerson = CurrentPerson->NextSiblingElement();     } =======================================================

以下是增加xml记录的操作,例如要增加 id 为 300,title 为 test3 的记录:

//创建节点对象      TiXmlElement *PersonElement = new TiXmlElement("prop");

//链接到根节点      RootElement ->LinkEndChild(PersonElement);

//创建节点对象的属性节点      TiXmlElement *IdElement = new TiXmlElement("id");      TiXmlElement *TitleElement =new TiXmlElement("title");

//将属性节点链接到子节点      PersonElement->LinkEndChild(IdElement);      PersonElement->LinkEndChild(TitleElement);

//创建属性对应数值对象

TiXmlText *idContent = new TiXmlText("300");

TiXmlText *titleContent = new TiXmlText("test3");

//将数值对象关联到属性节点      IdElement->LinkEndChild(idContent);      TitleElement->LinkEndChild(titleContent);

//保存到文件      myDocument->SaveFile(m_filefullPath.c_str());

=======================================================

以下是删除记录操作,例如要删除id为300 的记录:

//获取当前要删除的节点      TiXmlElement * childElement = 根据id从自己读取时缓存的数据中获得

//从根节点移除子节点      RootElement->RemoveChild(childElement);

//保存文件      myDocument->SaveFile(m_filefullPath.c_str());

=======================================================

VC++ TinyXML的更多相关文章

  1. TinyXML 在vs2010 VC++使用

    1.下载TinyXML  http://www.grinninglizard.com/tinyxml/  解压缩,将以下六个文件复制到您的工程项目文件(跟您的cpp文件在一个目录下)中 tinyxml ...

  2. tinyxml学习2

    在TinyXML中,根据XML的各种元素来定义了一些类: TiXmlBase:整个TinyXML模型的基类. TiXmlAttribute:对应于XML中的元素的属性. TiXmlNode:对应于DO ...

  3. 转:VC解析XML文件-CMarkup的使用详解

    本篇文章是对VC解析XML文件-CMarkup的使用进行了详细的分析介绍,需要的朋友参考下 VC解析XML文件的工具有很多,CMarkup, tinyXML,还有IBM的,MS的等等. 据说tinyX ...

  4. VC++源文件编码

    目录 第1章源代码文件    1 1.1 研究思路    1 1.2 实验结果    3 1.3 #pragma setlocale    4 1.4 /source-charset    5 1.5 ...

  5. C++ TinyXml操作(含源码下载)

      前言 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译,使用TinyXML进行C++ XML解析,使用简单,容易上手.这个解析库的模型通过解析X ...

  6. XML解析器(TinyXML)的使用指南

    关于XML文件的解析方法的引导, 大家可以去试试这个工具(TinyXML) 1.首先下载TinyXML库的文件,这里给出链接,大家自己去下吧,记着要上国际http://prdownloads.sour ...

  7. 转载 VC轻松解析XML文件 - CMarkup类的使用方法

    VC轻松解析XML文件 - CMarkup类的使用方法http://www.cctry.com/thread-3866-1-1.html VC解析XML文件的工具有很多,CMarkup, tinyXM ...

  8. 【C/C++开发】TinyXml操作(含源码下载)

    前言 TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译,使用TinyXML进行C++ XML解析,使用简单,容易上手. 这个解析库的模型通过解析XM ...

  9. Windows 常用运行库下载 (DirectX、VC++、.Net Framework等)

    经常听到有朋友抱怨他的电脑运行软件或者游戏时提示缺少什么 d3dx9_xx.dll 或 msvcp71.dll.msvcr71.dll又或者是 .Net Framework 初始化之类的错误而无法正常 ...

随机推荐

  1. 18.Android之SharedPreferences数据存储学习

    SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...

  2. opencv笔记3:trackbar简单使用

    time:2015年 10月 03日 星期六 13:54:17 CST # opencv笔记3:trackbar简单使用 当需要测试某变量的一系列取值取值会产生什么结果时,适合用trackbar.看起 ...

  3. codeforces 720A:Closing ceremony

    Description The closing ceremony of Squanch Code Cup is held in the big hall with n × m seats, arran ...

  4. Chkrootkit Sourcecode Learning

    目录 . Chkrootkit Introduce . Source Code Frame . chklastlog.c . chkwtmp.c . ifpromisc.c . chkproc.c . ...

  5. poj1631Bridging signals(最长单调递增子序列 nlgn)

    Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12251   Accepted: 6687 ...

  6. FCK编辑器漏洞总结

    1.查看编辑器版本FCKeditor/_whatsnew.html————————————————————————————————————————————————————————————— 2. Ve ...

  7. SQL2005删除复制数据库的发布与订阅的方法(转载)

    SQL2005删除复制数据库的发布与订阅的方法 --在测试环境中恢复从正式数据库服务器 上备份下来的bak文件后,正式环境里数据库复制的发布.订阅也被带进来了,结果恢复的数据库无法更改表结构,直接删除 ...

  8. C++_Eigen函数库用法笔记——Matrix and Vector Arithmetic

    Addition and subtraction Scalar multiplication and division Transposition Matrix-matrix and matrix-v ...

  9. make -e install ,,,make命令的-e选项!

    -e, --environment-overrides Environment variables override makefiles.环境变量覆盖Makefile文件. 用这个时,一般都自己编写s ...

  10. GTP (GPRS隧道协议(GPRSTunnellingProtocol))

    GTP (GPRS隧道协议(GPRSTunnellingProtocol)) GTP (GPRS隧道协议(GPRSTunnellingProtocol)) 编辑 本词条缺少信息栏.名片图,补充相关内容 ...