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 = ;
static char szBuf[nBufSize] = { }; // 写操作
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 = ; i < ; ++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( - 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 = ; i < ; ++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( - 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;
} 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 ;
}

生成的示例文件:

<?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>

最新文章

  1. C++ std::queue
  2. python3.5批量修改文件名
  3. IOS开发基础知识--碎片40
  4. [转]响应式WEB设计学习(1)—判断屏幕尺寸及百分比的使用
  5. DestroyWindow函数注意事项
  6. saltstack实战4--综合练习1
  7. 了解undefined、null、NaN的区别
  8. POI2001 金矿
  9. Struts2 中拦截器和Action的调用关系(写的很好)
  10. AndroidUI开源组件库BottomView 第三方自定义UI控件
  11. DataGrid( 数据表格) 组件[4]
  12. qt槽函数中,窗口镶嵌窗口的问题,求解
  13. Windows Phone开发(4):框架和页
  14. 全部用startssl生成的证书,配置Apache使其支持SSL
  15. 来了解一下Mysql索引的相关知识:基础概念、性能影响、索引类型、创建原则、注意事项
  16. POJ-2485 Highways---最小生成树中最大边
  17. 关于最小生成树,拓扑排序、强连通分量、割点、2-SAT的一点笔记
  18. Python中函数和模块的体验与使用
  19. 记一次排查局网内的ARP包 “不存在的” MAC 地址及 “不存在的”IP 所发的ARP包
  20. Redis中RedisTemplate和Redisson管道的使用

热门文章

  1. tomcat + apache 动静分离
  2. 重建二叉树_C++
  3. 杭电oj2012-2021
  4. centos 7 编译mod_security
  5. qfish/Bee-Xcode-Template
  6. python 错误 error: invalid command &#39;egg_info&#39;
  7. mysql 故障整理
  8. ros move_base costmap 理解和实现动态窗口法避障
  9. Cookie和Session在Node.JS中的实践(三)
  10. 集群/分布式/微服务/SOA 转