zoukankan      html  css  js  c++  java
  • rapidxml的使用

    RapidXml

     原项目github网址:https://github.com/dwd/rapidxml
     测试代码github项目网址:git@github.com:Mr-jiayunfei/githubtest.git

    1 使用条件

    从github上下载源码,将文件目录包含在自己项目目录中,在源码文件中包含下面语句

     #include <rapidxml.hpp>
     #include <rapidxml_print.hpp>
     #include <rapidxml_utils.hpp>
     
     using namespace rapidxml;

    2 读取XML文件

     bool ReadXML(const std::string& xml_full_name) 
     {
     bool rv = false;
     
     try
     {
      rapidxml::file<> content(xml_full_name.c_str());
     
      xml_document<> doc;
      doc.parse<0>(content.data());
     
      xml_node<char>* catelognode = doc.first_node(u8"catalog");
      if (!catelognode) {
      return false;
      }
     
      for (auto it = catelognode->first_node("book"); it; it = it->next_sibling())
      {
      std::string bookid;
      xml_attribute<> *att = it->first_attribute(u8"id");
      if (att) {
      bookid = att->value();
      std::cout << "bookid:" << bookid << std::endl;
      }
     
      xml_node<char>* authornode = it->first_node(u8"author");
      if (!authornode) {
      return false;
      }
      std::string authorname;
      auto value = authornode->value();
      if (value) {
      if (strlen(value) != 0) {
      authorname = value;
      std::cout << "authorname:" << authorname << std::endl;
      }
      }
     
      xml_node<char>* titlenode = it->first_node(u8"title");
      if (!titlenode) {
      return false;
      }
      std::string titlename;
      value = titlenode->value();
      if (value) {
      if (strlen(value) != 0) {
      titlename = value;
      std::cout << "titlename:" << titlename << std::endl;
      }
      }
     
      xml_node<char>* genrenode = it->first_node(u8"genre");
      if (!genrenode) {
      return false;
      }
      std::string genrename;
      value = genrenode->value();
      if (value) {
      if (strlen(value) != 0) {
      genrename = value;
      std::cout << "genrename:" << genrename << std::endl;
      }
      }
     
      }
     }
     catch (const parse_error& e) {
      std::string error_info = u8"解析XML出错,";
      error_info += e.what();
      return false;
     }
     catch (const std::exception& e) {
      std::string error_info = u8"解析XML出错,";
      error_info += e.what();
      return false;
     }
     catch (...) {
      return false;
     }
     
     return true;
     }

    3 写入XML文件

     bool WriteXML(const std::string& xml_full_name) {
     xml_document<char> doc;
     
     ///声明
     xml_node<> *decl_node = doc.allocate_node(node_declaration);
     decl_node->append_attribute(doc.allocate_attribute(u8"version", u8"1.0"));
     decl_node->append_attribute(doc.allocate_attribute(u8"encoding", u8"UTF-8"));
     doc.append_node(decl_node);
     
     xml_node<> *catalog_node = doc.allocate_node(node_element,
      u8"catalog");
     doc.append_node(catalog_node);
     
     for (int i=0;i<3;++i)
     {
      xml_node<> *book_node = doc.allocate_node(node_element, u8"book ");
      book_node->append_attribute(doc.allocate_attribute(u8"id","boolid"));
     
      xml_node<> *author_node = doc.allocate_node(node_element, u8"author ");
      author_node->append_node(doc.allocate_node(node_data, "","authorname"));
      book_node->append_node(author_node);
     
      xml_node<> *title_node = doc.allocate_node(node_element, u8"title ");
      title_node->append_node(doc.allocate_node(node_data, "", "titlename"));
      book_node->append_node(title_node);
     
      xml_node<> *genre_node = doc.allocate_node(node_element, u8"genre ");
      genre_node->append_node(doc.allocate_node(node_data, "", "genrename"));
      book_node->append_node(genre_node);
     
      catalog_node->append_node(book_node);
     }
     
     std::ofstream out(xml_full_name);
     out << doc;
     out.close();
     
     doc.clear();
     
     return true;
     }

    4 修改XML文件

     bool ModifyNodeValue(const std::string& xml_full_name, const std::string& node_name, const std::string& node_value) {
     rapidxml::file<> content(xml_full_name.c_str());
     
     xml_document<> doc;
     doc.parse<rapidxml::parse_no_data_nodes>(content.data());
     
     xml_node<>* catelog_node = doc.first_node(u8"catalog");
     if (!catelog_node) {
      return false;
     }
     xml_node<>* book_node = catelog_node->first_node(u8"book");
     if (!book_node) {
      return false;
     }
     xml_node<>* author_node = book_node->first_node(u8"author");
     if (!author_node) {
      return false;
     }
     
     const std::string author_name = node_value;
     const char * text = doc.allocate_string(author_name.c_str(), strlen(author_name.c_str()));
     author_node->value(text);
     
     std::string data;
     rapidxml::print(std::back_inserter(data), doc);
     
     std::ofstream out(xml_full_name);
     out << data;
     out.close();
     }

     

  • 相关阅读:
    [bzoj2259][Oibh]新型计算机_Dijkstra
    [bzoj1660][Usaco2006 Nov]Bad Hair Day_单调栈
    [bzoj3943][Usaco2015 Feb]SuperBull_Kruskal
    [bzoj2131]免费的馅饼_树状数组
    [bzoj3932][CQOI2015]任务查询系统_主席树
    软件图标大全网站
    提示用户一直输入数字(默认为正整数),当用户输入end的时候显示当前输入数字中的最大值。
    打印多边形的菱形(for的嵌套)
    while循环问题(老师询问问题,学生回答。学生会了可以放学,或者老师讲了10遍,还是没有会的,被迫无奈也要放学。)
    while练习:输入一个班级的人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩。
  • 原文地址:https://www.cnblogs.com/LuckCoder/p/13020091.html
Copyright © 2011-2022 走看看