C++ 实现以xml的格式写入文件

C++ XML类
该类主要将xml中的标签分为两类,无内容标签统一称为父标签,有内容的就以键值对的方式直接输出。
后面可能会优化通过函数参数的方式管控层级关系,现在是通过类里自动记录层级深度来表示的。

#include <fstream>
#include <string>
#include <iostream>
#include <map>

class XmlWriter {
private:
    std::ofstream m_file;

public:
  
    XmlWriter(const std::string& filename) {
        m_file.open(filename.c_str());
        if (!m_file) {
            std::cout << "" << std::endl;
        }
        m_file << "<?xml version=\"1.0\" encoding = \"UTF-8\"?> \n";

        depth = 0;
    }

    ~XmlWriter() {
        if (m_file) {
            m_file.close();
        }
    }
  
    void addParentElement(const std::string& parentLable = "") {
        if (!parentLable.empty()) {
            if (depth) {
                for (int i = 0; i < depth; i++)
                    m_file << "  ";
            }
            m_file << "<" << parentLable << ">\n";
            depth++;
            m_LableMap[depth]= parentLable;
        }
    }

    void addLableKey(const std::string& key, const std::string& value = "") {
       
        if (!key.empty() && !value.empty()) {
            if (depth)
            {
                for (int i = 0; i < depth; i++)
                    m_file << "  ";
            }
            m_file << "<" << key << ">" << value << "</" << key << ">\n";
        }
    }


    void closeParentElement() {
        if (depth)
        {
            for (int i = 0; i < depth - 1 ; i++)
                m_file << "  ";
        }
        m_file << "</" << m_LableMap[depth] << ">\n";
        m_LableMap.erase(depth);
        depth--;
    }

private:
    int depth;
    std::map<int, std::string> m_LableMap;

};

int main(int argc, char * argv[])
{
	XmlWriter writer("mateDate.xml");
	writer.addParentElement("Person");
	writer.addLableKey("Age", "27"); 
	writer.addLableKey("name", "hzh");
	writer.addParentElement("friend");
	writer.addLableKey("name", "zjm");
	writer.addLableKey("Age", "22");
	writer.closeParentElement();
	writer.addLableKey("pay", "30w");
	writer.closeParentElement();
	
	return 0;
}

相关推荐

  1. C++ 实现xml格式写入文件

    2024-05-10 15:20:05       31 阅读
  2. 文件指定格式存储~BMP~C实现~FAT32格式

    2024-05-10 15:20:05       48 阅读
  3. 收到字符串写入xml并且将这个xml写入.zip文件

    2024-05-10 15:20:05       62 阅读
  4. Python中写入csv格式文件出现乱码解决方法

    2024-05-10 15:20:05       54 阅读
  5. c语言中文件读入处理写入实战

    2024-05-10 15:20:05       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-10 15:20:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-10 15:20:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-10 15:20:05       82 阅读
  4. Python语言-面向对象

    2024-05-10 15:20:05       91 阅读

热门阅读

  1. 单例类设计模式、call_once

    2024-05-10 15:20:05       34 阅读
  2. 迁移docker存储目录

    2024-05-10 15:20:05       31 阅读
  3. 329. 矩阵中的最长递增路径

    2024-05-10 15:20:05       33 阅读
  4. 新媒体昵称命名一定要注意的几点!

    2024-05-10 15:20:05       29 阅读
  5. 生物经济:生物技术的未来

    2024-05-10 15:20:05       29 阅读
  6. 十二届蓝桥杯Python组3月中/高级试题 第五题

    2024-05-10 15:20:05       32 阅读
  7. MySQL变量的浮点数问题处理

    2024-05-10 15:20:05       25 阅读
  8. Boolean test介绍

    2024-05-10 15:20:05       31 阅读
  9. oracle数据归档方案

    2024-05-10 15:20:05       26 阅读
  10. ==与===的区别

    2024-05-10 15:20:05       34 阅读