boost :: property_tree:解析复杂的xml结构

时间:2017-10-30 10:35:19

标签: c++ xml boost boost-propertytree boost-parameter

我想使用boost property_tree解析xml结构。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Txn ver="1.0">
    <TOpts tCount="1"  tformat="0"  ttimeout="10" />
    <TData>
        <Tvalue date="YYMMDD" time="HHMM" Ref="100"/>
    </TData>
    <TCustOpts>
        <Param name="SALE" value="xyz" />
    </TCustOpts>
</Txn>

我能够解析上面xml的第一个Topts字段,但对于TData&amp; TCustOpts字段,我没有得到正确的迭代和解析xml并面临异常的方法。有人可以为TData&amp; TCustOpts字段解析。 以下是我的代码供参考。

stringstream ssString;
boost::property_tree::ptree pt1;
ssString << xml;
boost::property_tree::read_xml(ssString, pt1);

string TxnVer = pt1.get<string>("Txn.<xmlattr>.ver");

boost::property_tree::ptree formats = pt1.get_child("Txn");
BOOST_FOREACH(boost::property_tree::ptree::value_type const& node, formats) {
    if (node.first == "TOpts") {
        const boost::property_tree::ptree & attributes = node.second.get_child("<xmlattr>");
        BOOST_FOREACH(boost::property_tree::ptree::value_type const& v, attributes) {
            if (v.first == "tCount") {
                std::cout << " tCount " << v.second.data() << endl;
            }
            else if (v.first == "tformat") {
                std::cout << " tformat" << v.second.data() << endl;
            }
            else if (v.first == "ttimeout") {
                std::cout << " ttimeout " << v.second.data() << endl;
            }
          }
    }
    else if (node.first == "TOpts")

    else if (node.first == "TCustOpts") {
        const boost::property_tree::ptree & attributes1 = node.second.get_child("<xmlattr>");
        BOOST_FOREACH(boost::property_tree::ptree::value_type const& s, attributes1) {
            if (s.first == "name"){
                std::cout << "name " << s.second.data() << endl;
            }
            else if (s.first == "value") {
                std::cout << "value " << s.second.data() << endl;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

好的,使用Property Tree解析信息时常用的反模式是&#34; loop frenzy&#34;。

以树格式存储键值对的整个想法是避免必须循环低级结构,而是使用方便的寻址(使用路径)。

另一种反模式是将所有解析都放在一个大函数中。我把事情分开了。

定义一些数据类型

让我们从定义一些数据类型开始,以保​​持我们的数据可管理性:

namespace Domain {
    struct TOpts {
        size_t count;
        std::string format;
        size_t timeout ;
    };

    struct TData {
        std::string date; // YYMMD
        std::string time; // HHMM
        size_t ref;
    };

    struct TCustOpts {
        std::multimap<std::string, std::string> params;
    };

    struct Txn {
        std::string version;
        TOpts opts;
        TData data;
        TCustOpts custom_opts;
    };
}

这是我们的转换&#34;域层&#34;。

让我们解析!

所以,这就是我如何编写解析代码:

namespace Parsing {
    // concrete parse functions
    void parse(Domain::TOpts& v, ptree const& pt) {
        v.count   = pt.get("<xmlattr>.tCount", 0);
        v.format  = pt.get("<xmlattr>.tformat", "0");
        v.timeout = pt.get("<xmlattr>.ttimeout", 0);
    }

    void parse(Domain::TData& v, ptree const& pt) {
        v.date = pt.get("Tvalue.<xmlattr>.date", "YYMMDD");
        v.time = pt.get("Tvalue.<xmlattr>.time", "HHMM");
        v.ref  = pt.get("Tvalue.<xmlattr>.Ref", 0);
    }

    void parse(Domain::TCustOpts& v, ptree const& pt) {
        for (auto& param : pt) {
            if (param.first != "Param")
                continue;

            v.params.emplace(
                param.second.get("<xmlattr>.name", "(anon)"),
                param.second.get("<xmlattr>.value", ""));
        }
    }

    // make any parse helper available optionally
    template <typename T>
    void parse_optional(T& v, boost::optional<ptree const&> pt) {
        if (pt) parse(v, *pt);
    }

    void parse(Domain::Txn& v, ptree const& pt) {
        v.version = pt.get("<xmlattr>.ver", "0.0");
        parse_optional(v.opts,        pt.get_child_optional("TOpts"));
        parse_optional(v.data,        pt.get_child_optional("TData"));
        parse_optional(v.custom_opts, pt.get_child_optional("TCustOpts"));
    }
}

唯一不那么直截了当的事情是parse_optional处理可能缺席的子树。

使用它:

int main() {
    boost::property_tree::ptree pt;
    {
        extern char const* xml;
        std::stringstream ss(xml);
        read_xml(ss, pt);
    }

    Domain::Txn transaction;
    Parsing::parse(transaction, pt.get_child("Txn"));

    std::cout << transaction; // complete roundtrip
}

奖金:往返

让我们保存相同的&#34;域&#34;类返回属性树,因此我们可以验证它是否有效:

namespace Writing { // for DEBUG/demo only
    void serialize(Domain::TOpts const& v, ptree& pt) {
        pt.put("<xmlattr>.tCount", v.count);
        pt.put("<xmlattr>.tformat", v.format);
        pt.put("<xmlattr>.ttimeout", v.timeout);
    }

    void serialize(Domain::TData const& v, ptree& pt) {
        pt.put("Tvalue.<xmlattr>.date", v.date);
        pt.put("Tvalue.<xmlattr>.time", v.time);
        pt.put("Tvalue.<xmlattr>.Ref", v.ref);
    }

    void serialize(Domain::TCustOpts const& v, ptree& pt) {
        for (auto& param : v.params) {
            auto& p = pt.add_child("Param", ptree{});
            p.put("<xmlattr>.name", param.first);
            p.put("<xmlattr>.value", param.second);
        }
    }

    void serialize(Domain::Txn const& v, ptree& pt) {
        auto& txn = pt.add_child("Txn", ptree{});
        txn.put("<xmlattr>.ver", v.version);
        serialize(v.opts,        txn.add_child("TOpts", ptree{}));
        serialize(v.data,        txn.add_child("TData", ptree{}));
        serialize(v.custom_opts, txn.add_child("TCustOpts", ptree{}));
    }
}

完全演示

此演示显示您原始的XML解析和序列化:

<强> Live On Coliru

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

using boost::property_tree::ptree;

namespace Domain {
    struct TOpts {
        size_t count;
        std::string format;
        size_t timeout ;
    };

    struct TData {
        std::string date; // YYMMD
        std::string time; // HHMM
        size_t ref;
    };

    struct TCustOpts {
        std::multimap<std::string, std::string> params;
    };

    struct Txn {
        std::string version;
        TOpts opts;
        TData data;
        TCustOpts custom_opts;
    };
}

namespace Parsing {
    // concrete parse functions
    void parse(Domain::TOpts& v, ptree const& pt) {
        v.count   = pt.get("<xmlattr>.tCount", 0);
        v.format  = pt.get("<xmlattr>.tformat", "0");
        v.timeout = pt.get("<xmlattr>.ttimeout", 0);
    }

    void parse(Domain::TData& v, ptree const& pt) {
        v.date = pt.get("Tvalue.<xmlattr>.date", "YYMMDD");
        v.time = pt.get("Tvalue.<xmlattr>.time", "HHMM");
        v.ref  = pt.get("Tvalue.<xmlattr>.Ref", 0);
    }

    void parse(Domain::TCustOpts& v, ptree const& pt) {
        for (auto& param : pt) {
            if (param.first != "Param")
                continue;

            v.params.emplace(
                param.second.get("<xmlattr>.name", "(anon)"),
                param.second.get("<xmlattr>.value", ""));
        }
    }

    // make any parse helper available optionally
    template <typename T>
    void parse_optional(T& v, boost::optional<ptree const&> pt) {
        if (pt) parse(v, *pt);
    }

    void parse(Domain::Txn& v, ptree const& pt) {
        v.version = pt.get("<xmlattr>.ver", "0.0");
        parse_optional(v.opts,        pt.get_child_optional("TOpts"));
        parse_optional(v.data,        pt.get_child_optional("TData"));
        parse_optional(v.custom_opts, pt.get_child_optional("TCustOpts"));
    }
}

namespace Writing { // for DEBUG/demo only
    void serialize(Domain::TOpts const& v, ptree& pt) {
        pt.put("<xmlattr>.tCount", v.count);
        pt.put("<xmlattr>.tformat", v.format);
        pt.put("<xmlattr>.ttimeout", v.timeout);
    }

    void serialize(Domain::TData const& v, ptree& pt) {
        pt.put("Tvalue.<xmlattr>.date", v.date);
        pt.put("Tvalue.<xmlattr>.time", v.time);
        pt.put("Tvalue.<xmlattr>.Ref", v.ref);
    }

    void serialize(Domain::TCustOpts const& v, ptree& pt) {
        for (auto& param : v.params) {
            auto& p = pt.add_child("Param", ptree{});
            p.put("<xmlattr>.name", param.first);
            p.put("<xmlattr>.value", param.second);
        }
    }

    void serialize(Domain::Txn const& v, ptree& pt) {
        auto& txn = pt.add_child("Txn", ptree{});
        txn.put("<xmlattr>.ver", v.version);
        serialize(v.opts,        txn.add_child("TOpts", ptree{}));
        serialize(v.data,        txn.add_child("TData", ptree{}));
        serialize(v.custom_opts, txn.add_child("TCustOpts", ptree{}));
    }
}

namespace { // for debug/demo only
    std::ostream& operator<<(std::ostream& os, Domain::Txn const& v) {
        ptree tmp;
        Writing::serialize(v, tmp);
        write_xml(os, tmp, boost::property_tree::xml_writer_make_settings<std::string>(' ', 4));
        return os;
    }
}

int main() {
    boost::property_tree::ptree pt;
    {
        extern char const* xml;
        std::stringstream ss(xml);
        read_xml(ss, pt);
    }

    Domain::Txn transaction;
    Parsing::parse(transaction, pt.get_child("Txn"));

    std::cout << transaction; // complete roundtrip
}

char const* xml = R"(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Txn ver="1.0">
    <TOpts tCount="1"  tformat="0"  ttimeout="10" />
    <TData>
        <Tvalue date="YYMMDD" time="HHMM" Ref="100"/>
    </TData>
    <TCustOpts>
        <Param name="SALE" value="xyz" />
    </TCustOpts>
</Txn>
)";

打印哪些:

<?xml version="1.0" encoding="utf-8"?>
<Txn ver="1.0">
    <TOpts tCount="1" tformat="0" ttimeout="10"/>
    <TData>
        <Tvalue date="YYMMDD" time="HHMM"/>
    </TData>
    <TCustOpts>
        <Param name="SALE" value="xyz"/>
    </TCustOpts>
</Txn>