使用pugixml创建并解析XML
来源:互联网 发布:php gzip 优缺点 编辑:程序博客网 时间:2024/05/28 01:35
创建并读取如下xml文件:
<?xml version="1.0" encoding="utf-8"?><Students><!--Students!--><Student id="1"><Name>MaZhe</Name><Age>18</Age></Student><Student id="2"><Name>Miki</Name><Age>19</Age></Student><Student id="3"><Name>Maki</Name><Age>20</Age></Student></Students>
代码实现:
#include <iostream>#include <string>#include <vector>#include <list>#include "pugixml.hpp"static const char* kFileName = "students2.xml";struct Student { int id; std::string name; int age; Student(int id_, const std::string& name_, int age_) : id(id_) , name(name_) , age(age_) { } void Print() const { std::cout << "Id: " << id << ", Name: " << name << ", Age: " << age << std::endl; }};using namespace std;// 创建XML。static void WriteData() { // Prepare data. std::list<Student> students; students.push_back(Student(1, "MaZhe", 18)); students.push_back(Student(2, "Miki", 19)); students.push_back(Student(3, "Maki", 20)); // Serialization. pugi::xml_document xdoc; pugi::xml_node xdec = xdoc.prepend_child(pugi::node_declaration); xdec.append_attribute("version").set_value("1.0"); xdec.append_attribute("encoding").set_value("utf-8"); pugi::xml_node xstudents = xdoc.append_child("Students"); xstudents.append_child(pugi::node_comment).set_value("Students!"); for (const Student& student : students) { pugi::xml_node xstudent = xstudents.append_child("Student"); pugi::xml_attribute xstudent_id = xstudent.append_attribute("id"); xstudent_id.set_value(student.id); pugi::xml_node xname = xstudent.append_child("Name"); xname.append_child(pugi::node_pcdata).set_value(student.name.c_str()); pugi::xml_node xage = xstudent.append_child("Age"); char buf[128] = { 0 }; sprintf(buf, "%d", student.age); xage.append_child(pugi::node_pcdata).set_value(buf); } xdoc.print(std::cout); xdoc.save_file(kFileName);}// 读取并解析XML。static void ReadData() { std::list<Student> students; pugi::xml_document xdoc; if (!xdoc.load_file(kFileName)) { return; } pugi::xml_node xstudents = xdoc.child("Students"); for (pugi::xml_node xstudent = xstudents.first_child(); xstudent != NULL; xstudent = xstudent.next_sibling()) { int id = xstudent.attribute("id").as_int(); pugi::xml_node xname = xstudent.child("Name"); std::string name = xname.text().as_string(); pugi::xml_node xage = xstudent.child("Age"); int age = xage.text().as_int(); students.push_back(Student(id, name, age)); } for (const Student& student : students) { student.Print(); }}int main() { WriteData(); ReadData(); return 0;}
阅读全文
0 0
- 使用pugixml创建并解析XML
- C/C++解析XML,pugixml库的使用
- c++ xml 解析库 pugixml
- pugixml库之xml解析库
- python 创建并解析XML
- pugixml使用
- C++跨平台XML解析类库-pugixml
- 创建并解析XML文件Java实例
- 使用jquery,返回xml,并解析xml
- 基于pugixml开源解析库的XML文档构建和解析
- Pugixml一种快速解析XML文件的开源解析库
- Pugixml一种快速解析XML文件的开源解析库《转载》
- pugixml使用教程
- pugixml库的使用
- pugixml使用总结
- pugixml库的使用
- 使用DOM解析并打印XML文档
- jquery使用ajax加载并解析xml
- RHCSA测试题
- Ajax提交表单时kindeditor编辑器中的内容如何赋值给textarea
- 11.2(周四)
- Java程序性能优化 读书笔记(十六)集合访问优化
- android打包,用户渠道,分享apk可以知道某个用户分享的
- 使用pugixml创建并解析XML
- 大话数据结构读书笔记(6)----散列表(哈希表)
- ssh框架整合步骤
- 数据结构与算法之树的孩子双亲存储结构的讲解
- MySQL 学习
- JavaScript-day15-个人学习笔记
- Vue 插槽分发内容
- Eclipse设置护眼背景色以及字体颜色
- 活动的生命周期