Tinyxml基本用法

这里介绍一下Tinyxml的基本用法,废话不多说了,直接上代码


#include <iostream>
#include "tinyxml.h"

int main() {
   
    // 创建XML文档对象
    TiXmlDocument doc;

    // 创建根元素
    TiXmlElement* root = new TiXmlElement("Root");
    doc.LinkEndChild(root);


    std::cout << "------------生成XML------------" << std::endl;
    // 创建子元素1
    TiXmlElement* element1 = new TiXmlElement("Element1");
    root->LinkEndChild(element1);
    // 设置子元素2的文本内容
    TiXmlText* text1 = new TiXmlText("中文测试");
    element1->LinkEndChild(text1);
    doc.Print();
    std::cout  << std::endl;

    std::cout << "------------增加元素后------------" << std::endl;
    // 创建子元素2
    TiXmlElement* element2 = new TiXmlElement("Element2");
    root->LinkEndChild(element2);

    // 设置子元素2的文本内容
    TiXmlText* text = new TiXmlText("Hello, World!");
    element2->LinkEndChild(text);
    
    doc.Print();
    std::cout  << std::endl;
    doc.SaveFile("example.xml");
    
    // 加载已存在的XML文件
    bool loaded = doc.LoadFile("example.xml");
    if (!loaded) {
   
        std::cout << "Failed to load XML file." << std::endl;
        return 0;
    }

    // 获取根元素
    root = doc.RootElement();

    std::cout << "------------修改元素后------------" << std::endl;
    // 修改节点的值
    TiXmlElement* element = root->FirstChildElement("Element2");
    if (element) {
   
        // 修改节点的文本内容
        TiXmlNode *pValue = NULL;
		pValue = element->FirstChild();
        if (pValue) {
   
            pValue->SetValue("LindM四楼");
        }
    } 
    doc.Print();
    std::cout  << std::endl;

    std::cout << "------------插入新元素后------------" << std::endl;
    // 插入新元素
    TiXmlElement* newElement = new TiXmlElement("NewElement");
    TiXmlText* newText = new TiXmlText("Inserted Element");
    newElement->LinkEndChild(newText);
    root->LinkEndChild(newElement);

    //使用 InsertEndChild 的时候注意要进行内存释放
    //root->InsertEndChild(*newElement);
    //delete newElement;

    doc.Print();
    std::cout  << std::endl;

    std::cout << "------------删除元素后------------" << std::endl;
    // 删除元素
    element = root->FirstChildElement("Element2");
    if (element) {
   
        root->RemoveChild(element);
    }
    doc.Print();
    std::cout  << std::endl;

    // 保存修改后的XML文件
    bool saved = doc.SaveFile("example.xml");
    if (saved) {
   
        std::cout << "XML file modified and saved successfully." << std::endl;
    } else {
   
        std::cout << "Failed to save modified XML file." << std::endl;
    }

    //delete root;
    return 0;
}

------------生成XML------------
<Root>
    <Element1>中文测试</Element1>
</Root>

------------增加元素后------------
<Root>
    <Element1>中文测试</Element1>
    <Element2>Hello, World!</Element2>
</Root>

------------修改元素后------------
<Root>
    <Element1>中文测试</Element1>
    <Element2>LindM四楼</Element2>
</Root>

------------插入新元素后------------
<Root>
    <Element1>中文测试</Element1>
    <Element2>LindM四楼</Element2>
    <NewElement>Inserted Element</NewElement>
</Root>

------------删除元素后------------
<Root>
    <Element1>中文测试</Element1>
    <NewElement>Inserted Element</NewElement>
</Root>

XML file modified and saved successfully.

顺便进行分析下是否有内存泄漏:

[root@cambricon /platform/work/run]# valgrind --leak-check=full --track-origins=
yes ./testxml 
==24071== Memcheck, a memory error detector
==24071== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==24071== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==24071== Command: ./testxml
==24071== 
------------生成XML------------
<Root>
    <Element1>中文测试</Element1>
</Root>

------------增加元素后------------
<Root>
    <Element1>中文测试</Element1>
    <Element2>Hello, World!</Element2>
</Root>

------------修改元素后------------
<Root>
    <Element1>中文测试</Element1>
    <Element2>LindM四楼</Element2>
</Root>

------------插入新元素后------------
<Root>
    <Element1>中文测试</Element1>
    <Element2>LindM四楼</Element2>
    <NewElement>Inserted Element</NewElement>
</Root>

------------删除元素后------------
<Root>
    <Element1>中文测试</Element1>
    <NewElement>Inserted Element</NewElement>
</Root>

XML file modified and saved successfully.
==24071== 
==24071== HEAP SUMMARY:
==24071==     in use at exit: 0 bytes in 0 blocks
==24071==   total heap usage: 25 allocs, 25 frees, 93,033 bytes allocated
==24071== 
==24071== All heap blocks were freed -- no leaks are possible
==24071== 
==24071== For lists of detected and suppressed errors, rerun with: -s
==24071== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

All heap blocks were freed – no leaks are possible

完美!!!

相关推荐

  1. Tinyxml基本

    2023-12-15 11:16:02       39 阅读
  2. ansible 基本

    2023-12-15 11:16:02       43 阅读
  3. Vue基本

    2023-12-15 11:16:02       20 阅读
  4. ElasticSearch基本

    2023-12-15 11:16:02       7 阅读
  5. pymysql的基本

    2023-12-15 11:16:02       37 阅读
  6. tar 命令基本

    2023-12-15 11:16:02       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-15 11:16:02       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-15 11:16:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-15 11:16:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-15 11:16:02       18 阅读

热门阅读

  1. 使用qemu在arm上模拟x86并运行docker

    2023-12-15 11:16:02       55 阅读
  2. 携程英语测评(已offer)

    2023-12-15 11:16:02       26 阅读
  3. TypeScript基础知识

    2023-12-15 11:16:02       31 阅读
  4. 数据结构-栈

    2023-12-15 11:16:02       34 阅读
  5. gitk查看被删除的单个文件的所有历史记录

    2023-12-15 11:16:02       43 阅读
  6. Vue学习笔记-Vue3中的shallowReactive和shallowRef

    2023-12-15 11:16:02       42 阅读