rapidxml库的使用示例

最近需要在c++引擎中使用xml解析及生成功能,对选择xml库的过程记录一下

xml benchmark

  • 参考 XML Benchmark Results 10.10.2009 的结果 初步选择使用RapidXML
  • 查看RapidXML自己的benchmark数据,性能接近strlen

使用示例

参考各种数据对比,决定使用RapidXML,解析及生成的示例代码如下

#include 
#include 
#include 
#include  // for stringstream

#include "rapidxml.hpp"
#include "rapidxml_print.hpp"

using namespace std;

void parse() {
    string s = 
    ""
        ""
            ""
                ""
                    ""
                ""
            ""
        ""
    "";

    vector xml(s.begin(), s.end());
    xml.emplace_back('\0');
    rapidxml::xml_document<> xml_doc;
    try {
        xml_doc.parse(xml.data());

        auto dump_attribute = [](rapidxml::xml_node<> * node) {
            for (rapidxml::xml_attribute<> * attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute()) {
                std::cout << "    attribute name=" << attribute->name() << " value=" << attribute->value() << endl;
            }
        };
        rapidxml::xml_node<>* xml4nlp = xml_doc.first_node("xml4nlp");
        for (rapidxml::xml_node<> * doc = xml4nlp->first_node("doc"); doc; doc = doc->next_sibling()) {
            for (rapidxml::xml_node<>* para = doc->first_node("para"); para; para = para->next_sibling()) {
                for (rapidxml::xml_node<>* sent = para->first_node("sent"); sent; sent = sent->next_sibling()) {
                    for (rapidxml::xml_node<>* word = sent->first_node("word"); word; word = word->next_sibling()) {
                        dump_attribute(word);
                    }
                }
            }
        }
    }
    catch (rapidxml::parse_error e) {
        std::cout << e.what() << std::endl;
    }
}

void generate() {
    // 1.DOM
    rapidxml::xml_document<> doc;

    // 2.node_declaration
    rapidxml::xml_node<>* declaration = doc.allocate_node(rapidxml::node_declaration);
    declaration->append_attribute(doc.allocate_attribute("version", "1.0"));
    declaration->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
    doc.append_node(declaration);

    // 3.node_element
    rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "xml4nlp");
    doc.append_node(root);

    // 4.node_comment
    rapidxml::xml_node<>* comment = doc.allocate_node(rapidxml::node_comment, 0, "This is a node for comment!");
    root->append_node(comment);

    rapidxml::xml_node<>* para = doc.allocate_node(rapidxml::node_element, "para");
    root->append_node(para);

    // 5.set attribute
    rapidxml::xml_node<>* sent = doc.allocate_node(rapidxml::node_element, "sent");
    para->append_node(sent);
    sent->append_attribute(doc.allocate_attribute("id", doc.allocate_string("0")));
    sent->append_attribute(doc.allocate_attribute("cont", doc.allocate_string("apple")));

    stringstream output;
    output << doc;
    cout << output.str() << endl;
}

int main(int argc, char** argv) {
    parse();
    //generate();
    return 0;
}

Reference

  • http://xmlbench.sourceforge.net/index.php?page=results.php
  • http://xmlbench.sourceforge.net/results/benchmark200910/index.html
  • http://rapidxml.sourceforge.net/
  • http://rapidxml.sourceforge.net/manual.html
  • https://www.cnblogs.com/MenAngel/p/11552588.html
  • https://stackoverflow.com/questions/2808022/how-to-parse-an-xml-file-with-rapidxml

你可能感兴趣的:(rapidxml库的使用示例)