tinyxml2开源库使用

news/2024/7/10 21:01:28 标签: 开源, qt, c++

源码下载:GitHub - leethomason/tinyxml2: TinyXML2 is a simple, small, efficient, C++ XML parser that can be easily integrated into other programs.

1.加载tinyxml2库

解压上面现在的压缩包,将tinyxml2.h/tinyxml2.cpp添加到项目工程当中,要使用该库时,只需要使用对于的头文件即可

#include "tinyxml2.h"

2.创建XML文件
int test_tinyxml2_create()
{
    const char *declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    tinyxml2::XMLDocument doc;
    tinyxml2::XMLError ret = doc.Parse(declaration);
    if (ret != 0)
    {
        fprintf(stderr, "fail to parse xml file: %s\n", declaration);
        return -1;
    }

    tinyxml2::XMLComment *comment = doc.NewComment("this is a xml test file");
    doc.InsertEndChild(comment);

    tinyxml2::XMLElement *root = doc.NewElement("Root");
    doc.InsertEndChild(root);

    // User
    tinyxml2::XMLElement *user = doc.NewElement("User");
    user->SetAttribute("Name", "fengbingchun");
    root->InsertEndChild(user);

    tinyxml2::XMLElement *blog = doc.NewElement("Blog");
    tinyxml2::XMLText *text1 = doc.NewText("CSDN");
    blog->InsertEndChild(text1);
    user->InsertEndChild(blog);

    tinyxml2::XMLElement *code = doc.NewElement("Code");
    tinyxml2::XMLText *text2 = doc.NewText("GitHub");
    code->InsertEndChild(text2);
    user->InsertEndChild(code);

    // Blog
    tinyxml2::XMLElement *blog2 = doc.NewElement("Blog");
    blog2->SetAttribute("Name", "CSDN");
    root->InsertEndChild(blog2);

    tinyxml2::XMLElement *addr = doc.NewElement("Address");
    tinyxml2::XMLText *text3 = doc.NewText("https://blog.csdn.net/fengbingchun");
    addr->InsertEndChild(text3);
    blog2->InsertEndChild(addr);

    tinyxml2::XMLElement *id = doc.NewElement("ID");
    tinyxml2::XMLText *text4 = doc.NewText("fengbingchun");
    id->InsertEndChild(text4);
    blog2->InsertEndChild(id);

    // Code
    tinyxml2::XMLElement *code2 = doc.NewElement("Code");
    code2->SetAttribute("Name", "GitHub");
    root->InsertEndChild(code2);

    tinyxml2::XMLElement *addr2 = doc.NewElement("Address");
    tinyxml2::XMLText *text5 = doc.NewText("https://github.com//fengbingchun");
    addr2->InsertEndChild(text5);
    code2->InsertEndChild(addr2);

    tinyxml2::XMLElement *repositories = doc.NewElement("Repositories");
    tinyxml2::XMLText *text6 = doc.NewText("27");
    repositories->InsertEndChild(text6);
    code2->InsertEndChild(repositories);

#ifdef _MSC_VER
    const char *file_name = "E:/GitCode/Messy_Test/testdata/test.xml";
#else
    const char *file_name = "testdata/test.xml";
#endif

    ret = doc.SaveFile(file_name);
    if (ret != 0)
    {
        fprintf(stderr, "fail to save xml file: %s\n", file_name);
        return -1;
    }

    return 0;
}

结果:

<?xml version="1.0" encoding="UTF-8"?>
<!--this is a xml test file-->
<Root>
    <User Name="fengbingchun">
        <Blog>CSDN</Blog>
        <Code>GitHub</Code>
    </User>
    <Blog Name="CSDN">
        <Address>https://blog.csdn.net/fengbingchun</Address>
        <ID>fengbingchun</ID>
    </Blog>
    <Code Name="GitHub">
        <Address>https://github.com//fengbingchun</Address>
        <Repositories>27</Repositories>
    </Code>
</Root>
3.解析XML文件
int test_tinyxml2_parse()
{
#ifdef _MSC_VER
    const char *file_name = "E:/GitCode/Messy_Test/testdata/test_tinyxml2.xml";
#else
    const char *file_name = "testdata/test_tinyxml2.xml";
#endif

    tinyxml2::XMLDocument doc;
    tinyxml2::XMLError ret = doc.LoadFile(file_name);
    if (ret != 0)
    {
        fprintf(stderr, "fail to load xml file: %s\n", file_name);
        return -1;
    }

    tinyxml2::XMLElement *root = doc.RootElement();
    fprintf(stdout, "root element name: %s\n", root->Name());

    // User
    tinyxml2::XMLElement *user = root->FirstChildElement("User");
    if (!user)
    {
        fprintf(stderr, "no child element: User\n");
        return -1;
    }
    fprintf(stdout, "user name: %s\n", user->Attribute("Name"));

    tinyxml2::XMLElement *blog = user->FirstChildElement("Blog");
    if (!blog)
    {
        fprintf(stderr, "no child element: Blog, in User\n");
        return -1;
    }
    fprintf(stdout, "blog value: %s\n", blog->GetText());
    fprintf(stdout, "code value: %s\n\n", user->FirstChildElement("Code")->GetText());

    // Blog
    tinyxml2::XMLElement *blog2 = root->FirstChildElement("Blog");
    if (!blog2)
    {
        fprintf(stderr, "no child element: Blog\n");
        return -1;
    }
    fprintf(stdout, "blog name: %s\n", blog2->Attribute("Name"));

    tinyxml2::XMLElement *addr = blog2->FirstChildElement("Address");
    if (!addr)
    {
        fprintf(stderr, "no child element: Address, in Blog\n");
        return -1;
    }
    fprintf(stdout, "address value: %s\n", addr->GetText());
    fprintf(stdout, "id value: %s\n\n", blog2->FirstChildElement("ID")->GetText());

    // Code
    tinyxml2::XMLElement *code = root->FirstChildElement("Code");
    if (!code)
    {
        fprintf(stderr, "no child element: Code\n");
        return -1;
    }
    fprintf(stdout, "code name: %s\n", code->Attribute("Name"));

    tinyxml2::XMLElement *addr2 = code->FirstChildElement("Address");
    if (!addr2)
    {
        fprintf(stderr, "no child element: Address, in Code\n");
        return -1;
    }
    fprintf(stdout, "address value: %s\n", addr2->GetText());
    fprintf(stdout, "repositories value: %s\n\n", code->FirstChildElement("Repositories")->GetText());

    return 0;
}


http://www.niftyadmin.cn/n/5392671.html

相关文章

Ubuntu中Python包的寻找路径

文章目录 一、Pyhon包的查找位置二、某个Python特定包的查找位置参考 一、Pyhon包的查找位置 ▶ [~]$ python -m site sys.path [/home/wangji,/usr/lib/python310.zip,/usr/lib/python3.10,/usr/lib/python3.10/lib-dynload,/home/wangji/.local/lib/python3.10/site-packag…

AI论文速读 | STG-LLM 大语言模型如何理解时空数据?

论文标题&#xff1a;How Can Large Language Models Understand Spatial-Temporal Data? 论文链接&#xff1a;https://arxiv.org/abs/2401.14192 作者&#xff1a;Lei Liu, Shuo Yu, Runze Wang, Zhenxun Ma, Yanming Shen&#xff08;申彦明&#xff09; 关键词&#xf…

leetcode初级算法(python)- 数组

文章目录 1.从排序数组中删除重复项2.买卖股票最佳时机23.旋转数组运行颠倒列表法整体移动元素块法4.存在重复运行包含判断法排序比较判断法运行集合判断法5.只出现一次的数字6.两个数组的交集27.移动零8.两数之和9.旋转图像这篇博客中的代码都是数组计算。 1.从排序数组中删除…

vscode 设置打开中断的默认工作目录/路径

vscode 设置打开终端的默认工作目录/路径** 文章目录 vscode 设置打开终端的默认工作目录/路径**打开vscode&#xff0c;打开设置UI 或是设置JSON文件&#xff0c;找到相关设置项方式1&#xff1a;通过打开settings.json的UI界面 设置:方式2&#xff1a;通过打开设置settings.j…

LabVIEW和Python开发微细车削控制系统

LabVIEW和Python开发微细车削控制系统 为满足现代精密加工的需求&#xff0c;开发了一套基于LabVIEW和Python的微细车削控制系统。该系统通过模块化设计&#xff0c;实现了高精度的加工控制和G代码的自动生成&#xff0c;有效提高了微细车削加工的自动化水平和编程效率。 项目…

【Redis】redis简介与安装

Redis 简介 Redis 是完全开源的&#xff0c;遵守 BSD 协议&#xff08;Berkeley Software Distribution 意思是"伯克利软件发行版&#xff09;&#xff0c;是一个高性能的 key-value 数据库。具有以下几个比较明显的特点&#xff1a; 性能极高 – Redis能读的速度可以达…

【C++进阶】STL容器--list底层剖析(迭代器封装)

目录 前言 list的结构与框架 list迭代器 list的插入和删除 insert erase list析构函数和拷贝构造 析构函数 拷贝构造 赋值重载 迭代器拷贝构造、析构函数实现问题 const迭代器 思考 总结 前言 前边我们了解了list的一些使用及其注意事项&#xff0c;今天我们进一步深入…

golang的map是如何扩容的【重点】

具体内容参考链接 https://zhuanlan.zhihu.com/p/616979764 Golang的map就是使用哈希表作为底层实现&#xff0c;map 实际上就是一个指针&#xff0c;指向hmap结构体。 Go 语言中的 map 在扩容时&#xff0c;会重新分配更大的内存空间&#xff0c;并将原有的键值对重新哈希到新…