rapidxml的简单使用
生活随笔
收集整理的這篇文章主要介紹了
rapidxml的简单使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Write
#include <iostream> #include <string> #include <fstream> #include "rapidxml.hpp" #include "rapidxml_print.hpp" /* xml Documnet Declaration---Node Element---Node Comment; //注釋 ---NodeAttribute; //屬性 Text; //內容 //層級收 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);//層級收// 根節點rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");doc.append_node(root);// 注釋節點1rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, 0, "all students info");root->append_node(comment1);// 普通節點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);// 注釋節點2rapidxml::xml_node<>* comment2 = doc.allocate_node(rapidxml::node_comment, 0, "all books info");root->append_node(comment2);// 普通節點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;//加結束符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中取得根節點rapidxml::xml_node<> *root = doc.first_node("root");// 遍歷students的子節點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的子節點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; }總結
以上是生活随笔為你收集整理的rapidxml的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UEditor 配置
- 下一篇: ueditor java_编辑器uedi