rapidxml学习

rapidxml学习参考:官网http://rapidxml.sourceforge.net/https://blog.csdn.net/wqvbjhc/article/details/7662931http://blog.sina.com.cn/s/blog_9b0604b40101o6fm.htmlrapidxml_print.hpp修改代码:#ifndefRAPIDXML_PRINT_HP…

大家好,又见面了,我是你们的朋友全栈君。

参考: 官网http://rapidxml.sourceforge.net/
https://blog.csdn.net/wqvbjhc/article/details/7662931
http://blog.sina.com.cn/s/blog_9b0604b40101o6fm.html

rapidxml_print.hpp修改代码:

#ifndef RAPIDXML_PRINT_HPP_INCLUDED

#define RAPIDXML_PRINT_HPP_INCLUDED



// Copyright (C) 2006, 2009 Marcin Kalicinski

// Version 1.13

// Revision $DateTime: 2009/05/13 01:46:17 $

//! \file rapidxml_print.hpp This file contains rapidxml printer implementation



#include "rapidxml.hpp"



// Only include streams if not disabled

#ifndef RAPIDXML_NO_STREAMS

    #include <ostream>

    #include <iterator>

#endif



namespace rapidxml

{



    ///

    // Printing flags



    const int print_no_indenting = 0x1;   //!< Printer flag instructing the printer to suppress indenting of XML. See print() function.



    ///

    // Internal



    //! \cond internal

    namespace internal

    {

        

        ///

        // Internal character operations

    

        // Copy characters from given range to given output iterator

        template<class OutIt, class Ch>

        inline OutIt copy_chars(const Ch *begin, const Ch *end, OutIt out)

        {

            while (begin != end)

                *out++ = *begin++;

            return out;

        }

        

        // Copy characters from given range to given output iterator and expand

        // characters into references (&lt; &gt; &apos; &quot; &amp;)

        template<class OutIt, class Ch>

        inline OutIt copy_and_expand_chars(const Ch *begin, const Ch *end, Ch noexpand, OutIt out)

        {

            while (begin != end)

            {

                if (*begin == noexpand)

                {

                    *out++ = *begin;    // No expansion, copy character

                }

                else

                {

                    switch (*begin)

                    {

                    case Ch('<'):

                        *out++ = Ch('&'); *out++ = Ch('l'); *out++ = Ch('t'); *out++ = Ch(';');

                        break;

                    case Ch('>'): 

                        *out++ = Ch('&'); *out++ = Ch('g'); *out++ = Ch('t'); *out++ = Ch(';');

                        break;

                    case Ch('\''): 

                        *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('p'); *out++ = Ch('o'); *out++ = Ch('s'); *out++ = Ch(';');

                        break;

                    case Ch('"'): 

                        *out++ = Ch('&'); *out++ = Ch('q'); *out++ = Ch('u'); *out++ = Ch('o'); *out++ = Ch('t'); *out++ = Ch(';');

                        break;

                    case Ch('&'): 

                        *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';'); 

                        break;

                    default:

                        *out++ = *begin;    // No expansion, copy character

                    }

                }

                ++begin;    // Step to next character

            }

            return out;

        }



        // Fill given output iterator with repetitions of the same character

        template<class OutIt, class Ch>

        inline OutIt fill_chars(OutIt out, int n, Ch ch)

        {

            for (int i = 0; i < n; ++i)

                *out++ = ch;

            return out;

        }



        // Find character

        template<class Ch, Ch ch>

        inline bool find_char(const Ch *begin, const Ch *end)

        {

            while (begin != end)

                if (*begin++ == ch)

                    return true;

            return false;

        }



        ///

        // Internal printing operations

    template<class OutIt, class Ch>

        OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);    

    template<class OutIt, class Ch>
    OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);

    template<class OutIt, class Ch>

        OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);

    template<class OutIt, class Ch>

        OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);

    template<class OutIt, class Ch>

        OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
    template<class OutIt, class Ch>

         OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
    template<class OutIt, class Ch>

         OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
    template<class OutIt, class Ch>

         OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);


    // Print children of the node                               

        template<class OutIt, class Ch>

        inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent)

        {

            for (xml_node<Ch> *child = node->first_node(); child; child = child->next_sibling())

                out = print_node(out, child, flags, indent);

            return out;

        }

    

        // Print node

        template<class OutIt, class Ch>

        inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)

        {

            // Print proper node type

            switch (node->type())

            {



            // Document

            case node_document:

                out = print_children(out, node, flags, indent);

                break;



            // Element

            case node_element:

                out = print_element_node(out, node, flags, indent);

                break;

            

            // Data

            case node_data:

                out = print_data_node(out, node, flags, indent);

                break;

            

            // CDATA

            case node_cdata:

                out = print_cdata_node(out, node, flags, indent);

                break;



            // Declaration

            case node_declaration:

                out = print_declaration_node(out, node, flags, indent);

                break;



            // Comment

            case node_comment:

                out = print_comment_node(out, node, flags, indent);

                break;

            

            // Doctype

            case node_doctype:

                out = print_doctype_node(out, node, flags, indent);

                break;



            // Pi

            case node_pi:

                out = print_pi_node(out, node, flags, indent);

                break;



                // Unknown

            default:

                assert(0);

                break;

            }

            

            // If indenting not disabled, add line break after node

            if (!(flags & print_no_indenting))

                *out = Ch('\n'), ++out;



            // Return modified iterator

            return out;

        }

        

        

        // Print attributes of the node

        template<class OutIt, class Ch>

        inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int flags)

        {

            for (xml_attribute<Ch> *attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())

            {

                if (attribute->name() && attribute->value())

                {

                    // Print attribute name

                    *out = Ch(' '), ++out;

                    out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);

                    *out = Ch('='), ++out;

                    // Print attribute value using appropriate quote type

                    if (find_char<Ch, Ch('"')>(attribute->value(), attribute->value() + attribute->value_size()))

                    {

                        *out = Ch('\''), ++out;

                        out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('"'), out);

                        *out = Ch('\''), ++out;

                    }

                    else

                    {

                        *out = Ch('"'), ++out;

                        out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('\''), out);

                        *out = Ch('"'), ++out;

                    }

                }

            }

            return out;

        }



        // Print data node

        template<class OutIt, class Ch>

        inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)

        {

            assert(node->type() == node_data);

            if (!(flags & print_no_indenting))

                out = fill_chars(out, indent, Ch('\t'));

            out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);

            return out;

        }



        // Print data node

        template<class OutIt, class Ch>

        inline OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)

        {

            assert(node->type() == node_cdata);

            if (!(flags & print_no_indenting))

                out = fill_chars(out, indent, Ch('\t'));

            *out = Ch('<'); ++out;

            *out = Ch('!'); ++out;

            *out = Ch('['); ++out;

            *out = Ch('C'); ++out;

            *out = Ch('D'); ++out;

            *out = Ch('A'); ++out;

            *out = Ch('T'); ++out;

            *out = Ch('A'); ++out;

            *out = Ch('['); ++out;

            out = copy_chars(node->value(), node->value() + node->value_size(), out);

            *out = Ch(']'); ++out;

            *out = Ch(']'); ++out;

            *out = Ch('>'); ++out;

            return out;

        }



        // Print element node

        template<class OutIt, class Ch>

        inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)

        {

            assert(node->type() == node_element);



            // Print element name and attributes, if any

            if (!(flags & print_no_indenting))

                out = fill_chars(out, indent, Ch('\t'));

            *out = Ch('<'), ++out;

            out = copy_chars(node->name(), node->name() + node->name_size(), out);

            out = print_attributes(out, node, flags);

            

            // If node is childless

            if (node->value_size() == 0 && !node->first_node())

            {

                // Print childless node tag ending

                *out = Ch('/'), ++out;

                *out = Ch('>'), ++out;

            }

            else

            {

                // Print normal node tag ending

                *out = Ch('>'), ++out;



                // Test if node contains a single data node only (and no other nodes)

                xml_node<Ch> *child = node->first_node();

                if (!child)

                {

                    // If node has no children, only print its value without indenting

                    out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);

                }

                else if (child->next_sibling() == 0 && child->type() == node_data)

                {

                    // If node has a sole data child, only print its value without indenting

                    out = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);

                }

                else

                {

                    // Print all children with full indenting

                    if (!(flags & print_no_indenting))

                        *out = Ch('\n'), ++out;

                    out = print_children(out, node, flags, indent + 1);

                    if (!(flags & print_no_indenting))

                        out = fill_chars(out, indent, Ch('\t'));

                }



                // Print node end

                *out = Ch('<'), ++out;

                *out = Ch('/'), ++out;

                out = copy_chars(node->name(), node->name() + node->name_size(), out);

                *out = Ch('>'), ++out;

            }

            return out;

        }



        // Print declaration node

        template<class OutIt, class Ch>

        inline OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)

        {

            // Print declaration start

            if (!(flags & print_no_indenting))

                out = fill_chars(out, indent, Ch('\t'));

            *out = Ch('<'), ++out;

            *out = Ch('?'), ++out;

            *out = Ch('x'), ++out;

            *out = Ch('m'), ++out;

            *out = Ch('l'), ++out;



            // Print attributes

            out = print_attributes(out, node, flags);

            

            // Print declaration end

            *out = Ch('?'), ++out;

            *out = Ch('>'), ++out;

            

            return out;

        }



        // Print comment node

        template<class OutIt, class Ch>

        inline OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)

        {

            assert(node->type() == node_comment);

            if (!(flags & print_no_indenting))

                out = fill_chars(out, indent, Ch('\t'));

            *out = Ch('<'), ++out;

            *out = Ch('!'), ++out;

            *out = Ch('-'), ++out;

            *out = Ch('-'), ++out;

            out = copy_chars(node->value(), node->value() + node->value_size(), out);

            *out = Ch('-'), ++out;

            *out = Ch('-'), ++out;

            *out = Ch('>'), ++out;

            return out;

        }



        // Print doctype node

        template<class OutIt, class Ch>

        inline OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)

        {

            assert(node->type() == node_doctype);

            if (!(flags & print_no_indenting))

                out = fill_chars(out, indent, Ch('\t'));

            *out = Ch('<'), ++out;

            *out = Ch('!'), ++out;

            *out = Ch('D'), ++out;

            *out = Ch('O'), ++out;

            *out = Ch('C'), ++out;

            *out = Ch('T'), ++out;

            *out = Ch('Y'), ++out;

            *out = Ch('P'), ++out;

            *out = Ch('E'), ++out;

            *out = Ch(' '), ++out;

            out = copy_chars(node->value(), node->value() + node->value_size(), out);

            *out = Ch('>'), ++out;

            return out;

        }



        // Print pi node

        template<class OutIt, class Ch>

        inline OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)

        {

            assert(node->type() == node_pi);

            if (!(flags & print_no_indenting))

                out = fill_chars(out, indent, Ch('\t'));

            *out = Ch('<'), ++out;

            *out = Ch('?'), ++out;

            out = copy_chars(node->name(), node->name() + node->name_size(), out);

            *out = Ch(' '), ++out;

            out = copy_chars(node->value(), node->value() + node->value_size(), out);

            *out = Ch('?'), ++out;

            *out = Ch('>'), ++out;

            return out;

        }



    }

    //! \endcond



    ///

    // Printing



    //! Prints XML to given output iterator.

    //! \param out Output iterator to print to.

    //! \param node Node to be printed. Pass xml_document to print entire document.

    //! \param flags Flags controlling how XML is printed.

    //! \return Output iterator pointing to position immediately after last character of printed text.

    template<class OutIt, class Ch> 

    inline OutIt print(OutIt out, const xml_node<Ch> &node, int flags = 0)

    {

        return internal::print_node(out, &node, flags, 0);

    }



#ifndef RAPIDXML_NO_STREAMS



    //! Prints XML to given output stream.

    //! \param out Output stream to print to.

    //! \param node Node to be printed. Pass xml_document to print entire document.

    //! \param flags Flags controlling how XML is printed.

    //! \return Output stream.

    template<class Ch> 

    inline std::basic_ostream<Ch> &print(std::basic_ostream<Ch> &out, const xml_node<Ch> &node, int flags = 0)

    {

        print(std::ostream_iterator<Ch>(out), node, flags);

        return out;

    }



    //! Prints formatted XML to given output stream. Uses default printing flags. Use print() function to customize printing process.

    //! \param out Output stream to print to.

    //! \param node Node to be printed.

    //! \return Output stream.

    template<class Ch> 

    inline std::basic_ostream<Ch> &operator <<(std::basic_ostream<Ch> &out, const xml_node<Ch> &node)

    {

        return print(out, node);

    }



#endif



}



#endif

main.cpp

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <memory>
#include <sstream>
using namespace std;

//下面三个文件是本段代码需要的库文件
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp"
 

using namespace rapidxml;

int CreateXml();
int ReadAndChangeXml();

int main()
{
    CreateXml();
    ReadAndChangeXml();
    cout << "hello" << endl;
    return 0;
}
//创建一个名称为config.xml文件
int CreateXml()
{
    xml_document<> doc;  
    xml_node<>* rot = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version='1.0' encoding='utf-8'"));
    doc.append_node(rot);
    xml_node<>* node =   doc.allocate_node(node_element,"config","information");  
    xml_node<>* color =   doc.allocate_node(node_element,"color",NULL);  
    doc.append_node(node);
    node->append_node(color);
    color->append_node(doc.allocate_node(node_element,"red","0.1"));
    color->append_node(doc.allocate_node(node_element,"green","0.1"));
    color->append_node(doc.allocate_node(node_element,"blue","0.1"));
    color->append_node(doc.allocate_node(node_element,"alpha","1.0"));
 
    xml_node<>* size =   doc.allocate_node(node_element,"size",NULL); 
    size->append_node(doc.allocate_node(node_element,"x","640"));
    size->append_node(doc.allocate_node(node_element,"y","480"));
    node->append_node(size);
 
    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.xml");
    out << doc;
}

//读取并修改config.xml
int ReadAndChangeXml()
{
     file<> fdoc("config.xml");
    std::cout<<fdoc.data()<<std::endl;
    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明显有一个bug
    //那就是append_node(doc.allocate_node(node_element,"h","0"));的时候并不考虑该对象是否存在!
    xml_node<>* size = root->first_node("size");
    size->append_node(doc.allocate_node(node_element,"w","0"));
    size->append_node(doc.allocate_node(node_element,"h","0"));
 
    std::string text;
    rapidxml::print(std::back_inserter(text),doc,0);
 
    std::cout<<text<<std::endl;
 
    std::ofstream out("config.xml");
    out << doc;

}

编译:
g++ main.cpp -std=gnu++0x

转载于:https://www.cnblogs.com/shuqingstudy/p/11342991.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160926.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 用python做学生信息管理系统_python管理系统实例

    用python做学生信息管理系统_python管理系统实例Python面向对象版学员管理系统文章目录Python面向对象版学员管理系统目标一.系统需求二.准备程序文件2.1分析2.2创建程序文件三.书写程序3.1student.py3.1.2程序代码3.2managerSystem.py3.2.1定义类3.2.2管理系统框架3.3main.py3.4定义系统功能函数3.4.1添加功能3.4.2删除学员3.4.3修改学员信息3.4.5查询学员信息3.4.6显示所有学员信息3.4.7保存学员信息3.4.8加载学员信息四.总结

    2022年9月20日
    2
  • linux centos 权限审核 polkitd进程 简介[通俗易懂]

    linux centos 权限审核 polkitd进程 简介[通俗易懂]polkit是一个应用程序级别的工具集,通过定义和审核权限规则,实现不同优先级进程间的通讯:控制决策集中在统一的框架之中,决定低优先级进程是否有权访问高优先级进程。Polkit在系统层级进行权限控制,提供了一个低优先级进程和高优先级进程进行通讯的系统。和sudo等程序不同,Polkit并没有赋予进程完全的root权限,而是通过一个集中的策略系统进行更精细的授权。Polkit定义出一系列操作,例如运行GParted,并将用户按照群组或用户名进行划分,例如wheel群组用户。了.

    2022年6月15日
    97
  • 中标麒麟安装deb命令_麒麟源码

    中标麒麟安装deb命令_麒麟源码**中标麒麟NeoKylin-SDK里都有哪些库文件**下边是中标麒麟1-8和14的安装包内容。希望对中标麒麟开发的同学能有些帮助。[root@bogonNeoKylin-SDK]#shinstall.shPleaseselectwhichgroupyouwanttoinstall:1)C-development5)gnome-soft…

    2022年8月10日
    93
  • jsp+ajax_javascriptjavascript日

    jsp+ajax_javascriptjavascript日明后两天梁言兵老师来讲Ajax及其最近作过的一个真实的Ajax项目,所以,我今天讲解梁老师的课程所需要的一些前置知识。因为大家对Javascript不是很熟悉,所以我首先讲解Javascript的DHTML功能。本来入学考试要求大家很好地掌握Javascript的,但是大家都不能理解我们的苦衷,并没有专心去对待Javascript。想想我前两年强调javascript和css的重要性时,一些培训中

    2025年10月27日
    1
  • lunix部署_linux防火墙配置基本步骤

    lunix部署_linux防火墙配置基本步骤lunix重装好了以后都是空的一、创建相关文件夹二、将tomcat压缩包放到相关文件夹下三、解压tomcat压缩包,即安装过程四、修改安装好的tomcat文件夹名称为项目名称或者相关名称五,修改tomcat的/conf/server.xml文件,添加war包指向并可以将项目名去除登录

    2022年9月28日
    5
  • 水晶易表 Xcelsius 2008 安装指南 完美支持office2010(亲手体验)

    水晶易表 Xcelsius 2008 安装指南 完美支持office2010(亲手体验)

    2021年12月15日
    61

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号