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.
};
}