我想使用 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)