tinyxml2开源库使用

源码下载: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文件
cpp 复制代码
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 复制代码
<?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文件
cpp 复制代码
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;
}
相关推荐
扶尔魔ocy11 分钟前
【QT开发自制小工具】PDF/图片转excel---调用百度OCR API接口
qt·pdf·ocr·excel
zfenggo14 分钟前
c/c++ 无法跳转定义
c语言·开发语言·c++
图灵猿17 分钟前
【Lua之·Lua与C/C++交互·Lua CAPI访问栈操作】
c语言·c++·lua
魔众18 分钟前
一个桌面工具条系统,插件一键启动,快速扩展提高工作效率
开源·php·laravel·blog
A懿轩A1 小时前
C/C++ 数据结构与算法【树和二叉树】 树和二叉树,二叉树先中后序遍历详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·二叉树·
qh0526wy1 小时前
pyqt5冻结+分页表
开发语言·python·qt
hjxxlsx1 小时前
探索 C++ 自定义函数的深度与广度
开发语言·c++
利哥AI实例探险1 小时前
建筑工地AI安全检测系统:YOLO11数据标注训练与PyQt5界面设计应用开发
人工智能·qt·安全
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS安康旅游网站(JAVA毕业设计)
java·vue.js·spring boot·后端·kafka·开源·旅游
lijiachang0307182 小时前
设计模式(一):单例模式
c++·笔记·学习·程序人生·单例模式·设计模式·大学生