zoukankan      html  css  js  c++  java
  • pugixml 的常见读写操作

    pugixml github地址 : https://github.com/zeux/pugixml

    pugixml 可以在github上直接下载到源码,包括两个头文件(pugixml.hpp  pugiconfig.hpp) 和 一个源文件(pugixml.cpp)

    #include <iostream>
    #include <cstdint>
    #include "pugixml.hpp"
    
    static const int32_t nBufSize = 128;
    static char szBuf[nBufSize] = { 0 };
    
    // 写操作
    void Write(const char *szXmlFileName)
    {
        pugi::xml_document xmlDoc;
        pugi::xml_node nodeRoot = xmlDoc.append_child("root");
        // 声明
        pugi::xml_node pre = xmlDoc.prepend_child(pugi::node_declaration);
        pre.append_attribute("version") = "1.0";
        pre.append_attribute("encoding") = "utf-8";
    
        // 注释节点1
        pugi::xml_node nodeCommentStudents = nodeRoot.append_child(pugi::node_comment);
        nodeCommentStudents.set_value("all students info");
        // 普通节点1
        pugi::xml_node nodeStudents = nodeRoot.append_child("students");
        for(int32_t i = 0; i < 10; ++i)
        {
            sprintf_s(szBuf, nBufSize, "student_%02d", i);
            pugi::xml_node nodeStudent = nodeStudents.append_child("student");
            // 增加属性
            nodeStudent.append_attribute("name").set_value(szBuf);
            nodeStudent.append_attribute("score").set_value(100 - i);
        }
    
        // 注释节点2
        pugi::xml_node nodeCommentBooks = nodeRoot.append_child(pugi::node_comment);
        nodeCommentBooks.set_value("all books info");
        // 普通结点2
        pugi::xml_node nodeBooks = nodeRoot.append_child("books");
        for(int32_t i = 0; i < 10; ++i)
        {
            sprintf_s(szBuf, nBufSize, "book_%02d", i);
            pugi::xml_node nodeBook = nodeBooks.append_child("book");
            // 增加属性
            nodeBook.append_attribute("book").set_value(szBuf);
            nodeBook.append_attribute("price").set_value(50 - i);
        }
    
        xmlDoc.save_file(szXmlFileName, "	", 1U, pugi::encoding_utf8);
    }
    
    // 读操作
    void Read(const char *szXmlFileName)
    {
        pugi::xml_document xmlDoc;
        if(!xmlDoc.load_file(szXmlFileName, pugi::parse_default, pugi::encoding_utf8))
        {
            std::cout << "read " << szXmlFileName << " failed" << std::endl;
            return;
        }
    
        pugi::xml_node nodeRoot = xmlDoc.child("root");
        // 读取第一个节点
        for(pugi::xml_node node = nodeRoot.child("students").first_child(); node; node = node.next_sibling())
        {
            std::cout << "	" << node.attribute("name").value() << "," << node.attribute("score").value() << std::endl;
        }
        std::cout << std::endl;
        // 读取第二个节点
        for(pugi::xml_node node = nodeRoot.child("books").first_child(); node; node = node.next_sibling())
        {
            std::cout << "	" << node.attribute("book").value() << "," << node.attribute("price").value() << std::endl;
        }
    }
    
    int32_t main()
    {
        const char *szXmlFileName = "info.xml";
        Write(szXmlFileName);
        Read(szXmlFileName);
        std::cout << "finish" << std::endl;
        getchar();
        return 0;
    }

     生成的示例文件:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
        <!--all students info-->
        <students>
            <student name="student_00" score="100" />
            <student name="student_01" score="99" />
            <student name="student_02" score="98" />
            <student name="student_03" score="97" />
            <student name="student_04" score="96" />
            <student name="student_05" score="95" />
            <student name="student_06" score="94" />
            <student name="student_07" score="93" />
            <student name="student_08" score="92" />
            <student name="student_09" score="91" />
        </students>
        <!--all books info-->
        <books>
            <book book="book_00" price="50" />
            <book book="book_01" price="49" />
            <book book="book_02" price="48" />
            <book book="book_03" price="47" />
            <book book="book_04" price="46" />
            <book book="book_05" price="45" />
            <book book="book_06" price="44" />
            <book book="book_07" price="43" />
            <book book="book_08" price="42" />
            <book book="book_09" price="41" />
        </books>
    </root>
  • 相关阅读:
    汉字机内码的特点
    while(~scanf(..))的用法
    【C语言】八进制转十进制
    【C语言】按字典顺序排序
    【C语言】矩阵相乘
    【C语言】魔方阵
    【C语言】统计候选人的得票数
    【C语言】对输入的字符串中C关键词的查找统计
    20201231《信息安全导论》第十二周学习总结
    20201231《信息安全导论》第十一周学习总结
  • 原文地址:https://www.cnblogs.com/tangxin-blog/p/7067155.html
Copyright © 2011-2022 走看看