c++开源库rapidxm

本文深入讲解rapidxml库的使用,包括头文件引入、类型介绍、DOMTree操作、属性及结点管理等核心功能,适合XML解析处理的学习者。

  

1、引入头文件:

一般用到的头文件:

1 #include "rapidxml/rapidxml.hpp"
2 #include "rapidxml/rapidxml_utils.hpp" //rapidxml::file
3 #include "rapidxml/rapidxml_print.hpp" //rapidxml::print

2、部分类型介绍:

  类:xml_document  
  定义一个该类的对象doc 
  rapidxml::xml_document<> doc; 
  类的定义位置:rapidxml.hpp         类的成员函数: 

    1)parse(Ch *text) 将数据解析为DOM Tree
      使用时doc.parse(text);
      parseFlag指定格式,可以用’|’来组合使用
      常用的parseFlag:
      parseFlag为0表示默认的parseflag


      parse_comment_nodes表示带上xml中的注释
      parse_no_data_nodes在要修改结点值的时候要设置这个parseFlag


    2) clear() 清空DOM Tree
      此外xml_document继承自xml_node因此还具有xml_node的方法。

  3)获取DOM Tree结点rapidxml::xml_node<> *root; 类:xml_node 

    常用的类成员函数: 

      1)node_type type() const; 获取结点类型 获取的类型是枚举的
      2)Ch* name() const; 获取结点名
      3)std::size_t name_size() const; 获取结点名长度
      4)Ch* value() const; 获取结点值
      5)std::size_t value_size() const; 获取结点值长度
      7)xml_node* last_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 获取DOM Tree最后一个子结点的指针 

      8)xml_attribute* first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 获取结点的第一个属性指针 

      9)xml_attribute* next_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 获取结点的下一个属性指针 

      10)xml_attribute* last_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const;获取结点的最后一个属性指针 

        属性指针的格式见类xml_attribute 

      11)xml_node* previous_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const;获取上一个同级结点的指针

      12)xml_node* next_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 获取下一个同级结点的指针 

      13)xml_attribute* first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 获取第一个同级结点的指针 

      14)xml_attribute* last_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 获取最后一个同级结点的指针 

      15)void insert_node(xml_node< Ch > *where, xml_node< Ch > *child);在第一个参数指向的结点之前,

  

  4)获取属性
    类:xml_attribute
    定义于:rapidxml.hpp
    常用方法:
      1)xml_attribute *previous_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const;获取前一个属性
      2)xml_attribute *next_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const;获取后一个属性
  

  5)为一个新的属性或者结点分配空间
    1)为结点分配空间
      xml_node* allocate_node(node_type type, const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0);
    2)为属性分配空间
      xml_attribute* allocate_attribute(const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0);

namespace rapidxml
{
  // Forward declarations
  template<class Ch> class xml_node;
  template<class Ch> class xml_attribute;
  template<class Ch> class xml_document;

  //! Enumeration listing all node types produced by the parser.
  //! Use xml_node::type() function to query node type.
  enum node_type
  {

    //文档节点。 名称和值为空。
    node_document, //!< A document node. Name and value are empty.

    //元素节点。 名称包含元素名称。 值包含第一个数据节点的文本。

    node_element, //!< An element node. Name contains element name. Value contains text of first data node.

    //一个数据节点。 名称为空。 值包含数据文本。
    node_data, //!< A data node. Name is empty. Value contains data text.

    //一个CDATA节点。 名称为空。 值包含数据文本。
    node_cdata, //!< A CDATA node. Name is empty. Value contains data text.

    //评论节点。 名称为空。 值包含注释text
    node_comment, //!< A comment node. Name is empty. Value contains comment text.

    //声明节点。 名称和值为空。 声明参数(版本,编码和独立)在节点属性中。
    node_declaration, //!< A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalone) are in node attributes.

    //DOCTYPE节点。 名称为空。 值包含DOCTYPE文本。
    node_doctype, //!< A DOCTYPE node. Name is empty. Value contains DOCTYPE text.

    //PI节点。 名称包含目标。 值包含说明。
    node_pi //!< A PI node. Name contains target. Value contains instructions.
  };

}

 



转载于:https://www.cnblogs.com/Toney-01-22/p/9895062.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值