使用boost迭代xml文件

时间:2016-08-30 09:02:25

标签: c++ xml boost

我是boost和xml的新手,我正在尝试扫描xml文件并保存一些特定的值。

我读过this文章,我的问题是:如果xml包含多个<sked>,我该如何迭代它们?

也许

BOOST_FOREACH ()
   BOOST_FOREACH () // nested loop   

让我们说出给定的xml文件如下(目的是保存两个 ID):

<?xml version="1.0"? encoding="utf-8"?>
<arrayOfSked>
 <sked>
   <ID> 1 </ID>
   <version>2</version>
   <flight>
     <carrier>BA</carrier>
     <number>4001</number>
     <precision>0.1</precision>
   </flight>
   <flight cancelled="true">
     <carrier>BA</carrier>
     <number>4002</number>
     <precision>0.2</precision>
   </flight>
 </sked>

<sked>
   <ID> 2 </ID>
   <version>2</version>
   <flight>
     <carrier>BA</carrier>
     <number>4001</number>
     <precision>0.1</precision>
   </flight>
   <flight cancelled="true">
     <carrier>BA</carrier>
     <number>4002</number>
     <precision>0.2</precision>
   </flight>
 </sked>


</arrayOfSked>

1 个答案:

答案 0 :(得分:2)

从我的旧答案(boost::ptree find? or how to access deep arrays? C++)中汲取灵感,编写一个函数来访问树节点:

template <typename Tree, typename Out>
Out enumerate_nodes(Tree const& pt, typename Tree::path_type path, Out out) {
    if (path.empty())
        return out;

    if (path.single()) {
        auto name = path.reduce();
        for (auto& child : pt)
        {
            if (child.first == name)
                *out++ = child.second;
        }
    } else {
        auto head = path.reduce();
        for (auto& child : pt) {
            if (head == "*" || child.first == head) {
                out = enumerate_nodes(child.second, path, out);
            }
        }
    }

    return out;
}

现在您只需阅读文件:

using boost::property_tree::ptree;
ptree pt;

{
    std::ifstream ifs("input.txt");
    read_xml(ifs, pt);
}

收集航班:

std::vector<std::reference_wrapper<ptree const> > flights;
enumerate_nodes<ptree>(pt, "arrayOfSked.sked.flight", back_inserter(flights));

for (ptree const& flight : flights) {
    std::cout << "Canceled:\t" << (flight.get("<xmlattr>.cancelled", false)?"yes":"no") << "\n";
    std::cout << "Carrier:\t" << flight.get("carrier", "?") << "\n";
    std::cout << "Number:\t" << flight.get("number", 0) << "\n";
    std::cout << "Precision:\t" << flight.get("precision", 0.0) << "\n";
    std::cout << "------------------------------\n";
}

打印哪些:

Canceled:   no
Carrier:    BA
Number: 4001
Precision:  0.1
------------------------------
Canceled:   yes
Carrier:    BA
Number: 4002
Precision:  0.2
------------------------------
Canceled:   no
Carrier:    BA
Number: 4001
Precision:  0.1
------------------------------
Canceled:   yes
Carrier:    BA
Number: 4002
Precision:  0.2
------------------------------

完整演示在线

<强> Live On Coliru

#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

template <typename Tree, typename Out>
Out enumerate_nodes(Tree const& pt, typename Tree::path_type path, Out out) {
    if (path.empty())
        return out;

    if (path.single()) {
        auto name = path.reduce();
        for (auto& child : pt)
        {
            if (child.first == name)
                *out++ = child.second;
        }
    } else {
        auto head = path.reduce();
        for (auto& child : pt) {
            if (head == "*" || child.first == head) {
                out = enumerate_nodes(child.second, path, out);
            }
        }
    }

    return out;
}

int main() {
    using boost::property_tree::ptree;
    ptree pt;

    {
        std::ifstream ifs("input.txt");
        read_xml(ifs, pt);
    }

    std::vector<std::reference_wrapper<ptree const> > flights;
    enumerate_nodes<ptree>(pt, "arrayOfSked.sked.flight", back_inserter(flights));

    for (ptree const& flight : flights) {
        std::cout << "Canceled:\t" << (flight.get("<xmlattr>.cancelled", false)?"yes":"no") << "\n";
        std::cout << "Carrier:\t" << flight.get("carrier", "?") << "\n";
        std::cout << "Number:\t" << flight.get("number", 0) << "\n";
        std::cout << "Precision:\t" << flight.get("precision", 0.0) << "\n";
        std::cout << "------------------------------\n";
    }
}