日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rapidxml的简单使用

發(fā)布時(shí)間:2024/8/1 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rapidxml的简单使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Write

#include <iostream> #include <string> #include <fstream> #include "rapidxml.hpp" #include "rapidxml_print.hpp" /* xml Documnet Declaration---Node Element---Node Comment; //注釋 ---NodeAttribute; //屬性 Text; //內(nèi)容 //層級收 rapidxml所表示的類型都為類模板 */ static const int buf_len = 1024; static char buf[buf_len] = { 0 };void WriteXml(const char * file_name) {rapidxml::xml_document<> doc;// 聲明rapidxml::xml_node<>* declaration = doc.allocate_node(rapidxml::node_declaration);declaration->append_attribute(doc.allocate_attribute("version", "1.0"));declaration->append_attribute(doc.allocate_attribute("encoding", "utf-8"));doc.append_node(declaration);//層級收// 根節(jié)點(diǎn)rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");doc.append_node(root);// 注釋節(jié)點(diǎn)1rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, 0, "all students info");root->append_node(comment1);// 普通節(jié)點(diǎn)1rapidxml::xml_node<>* students = doc.allocate_node(rapidxml::node_element, "students");for (int i = 0; i < 10; ++i){rapidxml::xml_node<>* one_student = doc.allocate_node(rapidxml::node_element, "student");sprintf_s(buf, "student_%02d", i);//format// doc.allocate_string 的作用是將源字符串深拷貝一份one_student->append_attribute(doc.allocate_attribute("name", doc.allocate_string(buf)));one_student->append_attribute(doc.allocate_attribute("score", doc.allocate_string(std::to_string(100 - i).c_str())));students->append_node(one_student);}root->append_node(students);// 注釋節(jié)點(diǎn)2rapidxml::xml_node<>* comment2 = doc.allocate_node(rapidxml::node_comment, 0, "all books info");root->append_node(comment2);// 普通節(jié)點(diǎn)2rapidxml::xml_node<>* books = doc.allocate_node(rapidxml::node_element, "books");for (int i = 0; i < 10; ++i){rapidxml::xml_node<>* one_book = doc.allocate_node(rapidxml::node_element, "book");sprintf_s(buf, "book_%02d", i);// doc.allocate_string 的作用是將源字符串深拷貝一份one_book->append_attribute(doc.allocate_attribute("name", doc.allocate_string(buf)));one_book->append_attribute(doc.allocate_attribute("price", doc.allocate_string(std::to_string(50 - i).c_str())));books->append_node(one_book);}root->append_node(books);char *end = rapidxml::print(buf, doc, 0);//將xml文檔寫入buff*end = 0;//加結(jié)束符std::cout << buf;std::ofstream outfile(file_name, std::ios::out);if (outfile){outfile << buf;outfile.close();}}int main() {WriteXml("info.xml");return 0; }

Read

#include <iostream> #include <fstream> #include "rapidxml.hpp"static const int buf_len = 1024; static char buf[buf_len] = { 0 }; void ReadXml(const char * file_name) {std::ifstream infile(file_name, std::ios::in);if (!infile){return;}infile.read(buf, buf_len);std::cout << buf << std::endl;rapidxml::xml_document<> doc;//doc documentdoc.parse<0>(buf);//解析獲取xml文檔// 從xml中取得根節(jié)點(diǎn)rapidxml::xml_node<> *root = doc.first_node("root");// 遍歷students的子節(jié)點(diǎn)for (rapidxml::xml_node<> * node = root->first_node("students")->first_node(); node; node = node->next_sibling()){std::cout << node->first_attribute("name")->value() << ", "<< node->first_attribute("score")->value() << std::endl;}// 遍歷books的子節(jié)點(diǎn)for (rapidxml::xml_node<> * node = root->first_node("books")->first_node(); node; node = node->next_sibling()){std::cout << node->first_attribute("name")->value() << ", "<< node->first_attribute("price")->value() << std::endl;} }int main() {ReadXml("info.xml");return 0; }

總結(jié)

以上是生活随笔為你收集整理的rapidxml的简单使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。