转:TinyXM–优秀的C++ XML解析器

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);

Original: https://www.cnblogs.com/skyofbitbit/p/4769301.html
Author: bitbit
Title: 转:TinyXM–优秀的C++ XML解析器

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/536607/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • C++/服务器开发4天实战训练营

    第一天: 1.四种不同的方式来实现add函数 //面向过程 int add1(int a, int b) { return a + b; } //面向对象 class ADD{ p…

    C++ 2023年5月29日
    039
  • C++ 11 关键字:thread_local(转)

    thread_local 是 C++ 11 新引入的一种存储类型,它会影响变量的存储周期。 C++ 中有 4 种存储周期: 有且只有 thread_local 关键字修饰的变量具有…

    C++ 2023年5月29日
    043
  • 【面试攻略】C++面试-边锋

    2020-11-26-边锋 1.说说你以前的架构2.C++11特性,好像问到了这个constexpr https://www.jianshu.com/p/5480c4a35d1d3…

    C++ 2023年5月29日
    056
  • C++矩阵运算库推荐

    最近在几个地方都看到有人问C++下用什么矩阵运算库比较好,顺便做了个调查,做一些相关的推荐吧。主要针对稠密矩阵,有时间会再写一个稀疏矩阵的推荐。 Armadillo:C++下的Ma…

    C++ 2023年5月29日
    063
  • Qt 表格&列表数据驱动化(c++) 原创

    一、 Qt表格&列表 展示效果描述化与数据绑定驱动化 调用及解说如下: csharp;gutter:false; QStringList formats; formats….

    C++ 2023年5月29日
    064
  • VS Code C++ 代码格式化方法(clang-format)

    转自:https://blog.csdn.net/core571/article/details/82867932?depth_1-utm_source=distribute.pc…

    C++ 2023年5月29日
    0101
  • c++如何遍历删除map/vector里面的元素

    新技能Get! 对于c++里面的容器, 我们可以使用iterator进行方便的遍历. 但是当我们通过iterator对vector/map等进行修改时, 我们就要小心了, 因为操作…

    C++ 2023年5月29日
    081
  • [C++] 浅拷贝和深拷贝

    浅拷贝只是简单的值拷贝; 深拷贝需要重新分配空间。 系统默认的拷贝构造函数属于浅拷贝。 输出结果为: HelloHelloWorldWorld 为什么修改对象 m 的值,对象 n …

    C++ 2023年5月29日
    062
  • C++ 获取当前时间毫秒数

    在window环境下:1、精确到毫秒 include “stdafx.h” include include Original: https://www.cn…

    C++ 2023年5月29日
    050
  • C++检测和定位内存泄漏

    1、首先需要宏定义一下new运算符 解释: new(a, b, c) T; 会被解释成一个函数调用operator new(sizeof(T), a, b, c)。这是C++就有的…

    C++ 2023年5月29日
    068
  • libnode 0.4.0 发布,C++ 语言版的 Node.js

    libnode 0.4.0 支持 Windows ,提升了性能,libuv 更新到 0.10.17 版本,libj 更新到 0.8.2 版本。 libnode 是 C++ 语言版的…

    C++ 2023年5月29日
    062
  • 面向对象C++编程与实践考试答案与解析

    一、选择题(每题2分,共2×20=40分) (1) 以下不能对数组 a 进行正确初始化的语句是( C )。A. int a[2][3] = { 0 };B. int a[ ][3]…

    C++ 2023年5月29日
    055
  • C++中的POD类型

    参考 定义 总结与理解 参考 https://en.cppreference.com/w/cpp/named_req/PODType 定义 知识的搬运工,以下内容抄的,虽然是硬性定…

    C++ 2023年5月29日
    062
  • C++智能指针原理

    简介 智能指针就是对指针进行封装,使其提供特有的功能。 unique_ptr:封装了原始指针使其只能在同一时刻被同一对象拥有,并且在离开作用域时会自动销毁。 shared_ptr:…

    C++ 2023年5月29日
    075
  • 配置 Windows 下的 nodejs C++ 模块编译环境

    Python 安装 python-2.7.7.msi iso 虚拟磁盘 安装 DTLite4491-0356.exe Windows XP 用 DTLite 打开 VS2010Ex…

    C++ 2023年5月29日
    052
  • 当C++遇到iOS应用开发—Dict集合

    在Object-c中,字典(KEY/VALUE)使用NSDictionary 和NSMutableDictionary(可变长)。使用语法如下: NSDictionary *dic…

    C++ 2023年5月29日
    057
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球