首页 > 代码库 > pugixml 的常见读写操作

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, "\t", 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;
    }
    xmlDoc.load_file(szXmlFileName, pugi::parse_default, pugi::encoding_utf8);

    pugi::xml_node nodeRoot = xmlDoc.child("root");
    // 读取第一个节点
    for(pugi::xml_node node = nodeRoot.child("students").first_child(); node; node = node.next_sibling())
    {
        std::cout << "\t" << 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 << "\t" << 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>

 

pugixml 的常见读写操作