RapidXml的使用

RapidXml Manual: http://rapidxml.sourceforge.net/manual.html

DOM解析是将XML文件全部载入,组装成一颗DOM树,然后通过节点以及节点之间的关系来解析XML文件。

RapidXml是一个使用C++编写的XML DOM解析工具包,整个解析工具包包含在一个头文件中,所以使用时不用编译也不用连接。只要包含rapidxml中的三个头文件即可。

RapidXml 试图成为最快的 XML DOM 解析工具包,同时保证解析结果的可用性、可移植性以及与 W3C 标准的兼容性。在操作同一数据时,其解析速度接近于 strlen() 函数。

  • 解析:

以下代码使用RapidXml解析一段以0结束的字符串text:

1 using namespace rapidxml;
2 xml_document<> doc;    // character type defaults to char
3 doc.parse<0>(text);    // 0 means default parse flags

其中,doc为解析得到的DOM tree的根节点。由于所有的RapidXml接口都包含在rapixml,所以用户需要使用这个名字空间。类xml_document代表了DOM结构的根,它公开继承了xml_node和memory_pool。xml_document::parse()的模板参数用来标识解析标志,使用它可以对解析器的行为进行调整(这里我也不太明白,调整什么?)。这个标志必须是编译时的常数。

  • Accessing DOM Tree:

使用xml_node和xml_attribute类中的方法访问DOM tree。

1 cout << "Name of my first node is: " << doc.first_node()->name() << "\n";
2 xml_node<> *node = doc.first_node("foobar");
3 cout << "Node foobar has value " << node->value() << "\n";
4 for (xml_attribute<> *attr = node->first_attribute();
5      attr; attr = attr->next_attribute())
6 {
7     cout << "Node foobar has attribute " << attr->name() << " ";
8     cout << "with value " << attr->value() << "\n";
9 }
  • Modifying DOM Tree:

下例为创建一个HTML文档,它唯一的内容是一个google.com的链接( Google):

xml_document<> doc;
xml_node<> *node = doc.allocate_node(node_element, "a", "Google");
doc.append_node(node);
xml_attribute<> *attr = doc.allocate_attribute("href", "google.com");
node->append_attribute(attr);

nodes和attributes并不真正拥有文章中节点和属性的名字及值,因为它们只是存储了指向源文中某个位置的指针。所以,当为一个节点分配名字和值的时候,必须确保待这些字符串有合适的生命周期。最简单的方法是从xml_document memory pool中分配字符串。当然,在上面例子中没有必要这么做,因为这里使用了字符常量。下面的代码使用了memory_pool::allocate_string()方法分配节点名字(这样它将和文档具有相同的生命周期)给新的节点:

xml_document<> doc;
char *node_name = doc.allocate_string(name);        // Allocate string and copy name into it
xml_node<> *node = doc.allocate_node(node_element, node_name);  // Set node name to node_name
  • Printing XML

将xml_document和xml_node的对象写入一XML的string里,可以使用在rapidxml_print.hpp头文件中定义的print()函数或者操作符<<。

using namespace rapidxml;
xml_document<> doc;    // character type defaults to char
// ... some code to fill the document

// Print to stream using operator <<
std::cout << doc;   

// Print to stream using print function, specifying printing flags
print(std::cout, doc, 0);   // 0 means default printing flags

// Print to string using output iterator
std::string s;
print(std::back_inserter(s), doc, 0);

// Print to memory buffer using output iterator
char buffer[4096];                      // You are responsible for making the buffer large enough!
char *end = print(buffer, doc, 0);      // end contains pointer to character after last printed character
*end = 0;                               // Add string terminator after XML

原文链接: https://www.cnblogs.com/hihilary/archive/2012/11/18/2772983.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/69905

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午2:01
下一篇 2023年2月9日 下午2:01

相关推荐