小编San*_*dar的帖子

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

我想使用 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>
Run Code Online (Sandbox Code Playgroud)

我能够解析Topts上述 xml 的第一个字段,但是对于TData&TCustOpts字段,我没有得到正确的迭代和方法来解析 xml 并面临异常。有人可以为我提供正确的TData&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& …
Run Code Online (Sandbox Code Playgroud)

c++ xml boost boost-parameter boost-propertytree

0
推荐指数
1
解决办法
900
查看次数

标签 统计

boost ×1

boost-parameter ×1

boost-propertytree ×1

c++ ×1

xml ×1