zoukankan      html  css  js  c++  java
  • C++中rapidxml用法及例子

      rapidxml是一个快速的xml库,比tinyxml快了50-100倍。本文给出创建、读取、写入xml的源码。
      由于新浪博客不支持文本文件上传,在使用下面代码需要先下载 rapidxml,关于这个库的下载地址为:官方网站: http://download.csdn.net/detail/u013767393/8419707,这个库源码一共只有四个文件分别是:
      C++中rapidxml用法及例子(源码)
      关于raidxml资料相关的介绍网上有很多资料,在这里我就不重复介绍了,下面直接贴代码:
      
    #include "stdafx.h"
    #include "stdlib.h"
    #include <iostream>
    
    //下面三个文件是本段代码需要的库文件
    #include "rapidxml/rapidxml.hpp"       
    #include "rapidxml/rapidxml_utils.hpp"
    #include "rapidxml/rapidxml_print.hpp"
    
    int CreateXml();
    int ReadAndChangeXml();
    int _tmain(int argc, _TCHAR* argv[])
    {
        //测试用例
        CreateXml();
        //测试用例
        ReadAndChangeXml();
        system("pause");
        return 0;
    }
    //创建一个名称为config2.xml文件
    int CreateXml()
    {
        rapidxml::xml_document<> doc;
        rapidxml::xml_node<>* rot = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version='1.0' encoding='utf-8'"));
        doc.append_node(rot);
        rapidxml::xml_node<>* node = doc.allocate_node(rapidxml::node_element,"config","information");
        doc.append_node(node);
        rapidxml::xml_node<>* color = doc.allocate_node(rapidxml::node_element,"color",NULL);
        node->append_node(color);
        color->append_node(doc.allocate_node(rapidxml::node_element,"red","0.1"));
        color->append_node(doc.allocate_node(rapidxml::node_element,"green","0.1"));
        color->append_node(doc.allocate_node(rapidxml::node_element,"blue","0.1"));
        color->append_node(doc.allocate_node(rapidxml::node_element,"alpha","1.0"));
        rapidxml::xml_node<>* size = doc.allocate_node(rapidxml::node_element,"size",NULL);
        size->append_node(doc.allocate_node(rapidxml::node_element,"x","640"));
        size->append_node(doc.allocate_node(rapidxml::node_element,"y","480"));
        node->append_node(size);
        rapidxml::xml_node<>* mode = doc.allocate_node(rapidxml::node_element,"mode","screen mode");
        mode->append_attribute(doc.allocate_attribute("fullscreen","false"));
        node->append_node(mode);
        std::string text;
        rapidxml::print(std::back_inserter(text), doc, 0);
        std::cout<<text<<std::endl;
        std::ofstream out("../config/config1.xml");
        out << doc;
    
        return 0;
    }
    
    
    //读取并修改config3.xml
    int ReadAndChangeXml()
    {
         rapidxml::file<> fdoc("../config/config2.xml");
         std::cout<<fdoc.data()<<std::endl;
         rapidxml::xml_document<> doc;
         doc.parse<0>(fdoc.data());
         std::cout<<doc.name()<<std::endl;
         //! 获取根节点
         rapidxml::xml_node<>* root = doc.first_node();
        std::cout<<root->name()<<std::endl;
        //! 获取根节点第一个节点
        rapidxml::xml_node<>* node1 = root->first_node();
        std::cout<<node1->name()<<std::endl;
        rapidxml::xml_node<>* node11 = node1->first_node();
        std::cout<<node11->name()<<std::endl;
        std::cout<<node11->value()<<std::endl;
        //! 修改之后再次保存
        rapidxml::xml_node<>* size = root->first_node("size");
        size->append_node(doc.allocate_node(rapidxml::node_element,"w","1"));
        size->append_node(doc.allocate_node(rapidxml::node_element,"h","1"));
        std::string text;
        rapidxml::print(std::back_inserter(text),doc,0);
       std::cout<<text<<std::endl;
       std::ofstream out("../config/config2.xml");
       out << doc;
       
       return 0;
    }
  • 相关阅读:
    SPOJ375(树链剖分)
    最短路相关模板、总结
    Linux入门基础#2:Linux文件系统基本结构
    poj 2229 Sumsets (DP)
    Power BI for Office 365(七) Power BI站点
    HDU1045 Fire Net
    Android 系统搜索框(有浏览记录)
    Struts2 Action接收表单参数
    要注意的点
    复习昨天的,继续过Hard题目
  • 原文地址:https://www.cnblogs.com/MrYuan/p/4971165.html
Copyright © 2011-2022 走看看