zoukankan      html  css  js  c++  java
  • rapidxml使用

    以前都是用tinyxml,这次开发中解析xml配置文件像尝试一下rapidxml,据说效率很高。。。

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

    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() << "
    ";
    2 xml_node<> *node = doc.first_node("foobar");
    3 cout << "Node foobar has value " << node->value() << "
    ";
    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() << "
    ";
    9 }
    复制代码
    • Modifying DOM Tree:

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

    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
    复制代码

    包含必要的头文件
    #include "rapidxml.hpp"  
    创建文档对象
    rapidxml::xml_document<char> doc;  
    分析xml字符串,要求以''结尾
    std::string str(...);
    doc.parse<0>(const_cast<char *>(str.c_str()));  
    获取节点
    rapidxml::xml_node<char> * node = doc.first_node("node name");  
    遍历所有节点
    for(rapidxml::xml_node<char> * node = parent_node->first_node("node name");
        node != NULL;
        node = node->next_sibling())
    {
        ...
    }
      
    遍历所有属性
    for(rapidxml::xml_attribute<char> * attr = node->first_attribute("node name");
        attr != NULL;
        attr = attr->next_attribute())
    {
        ...
    }  
    获取属性值
    char * value = attr->value(); 

    1. // Load file to buffer.  
    2. rapid::xml_file<> fileLoad("test.xml");  
    3. char* content = fileLoad.data();  
    4. if (content) {  
    5.     // Load content to rapid.  
    6.     rapid::xml_document<> docParse;  
    7.     docParse.parse<0>(content);  
    8.   
    9.     // Parse node.  
    10.     rapid::xml_node<>node_root = docParse.first_node("item");  
    11.     ...   
    12. }  
    1. rapid::xml_docmenet<> docSave;  
    2.   
    3. // Generate root node.  
    4. rapid::xml_node<>* node_root = docSave.alloc_node(docSave.allocate_string("item"));  
    5. ...  
    6.   
    7. // Save to file.  
    8. std::ofstream fileSave;  
    9. fileSave << docSave;  
  • 相关阅读:
    event 事件 键盘控制div移动
    event 事件 div鼠标跟随
    获取坐标封装 getPos
    event 事件 clientX 和clientY 配合scrollTop使用, div跟着鼠标走
    event 事件 冒泡
    event 事件 坐标兼容
    event事件基础 document
    DOM 多字符搜索
    DOM search table 模糊搜索
    Reverse a sentence
  • 原文地址:https://www.cnblogs.com/kex1n/p/3190394.html
Copyright © 2011-2022 走看看