问题描述
我想使用boost property_tree解析以下xml结构。
I am want to parse below xml structure using boost property_tree.
<?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>
我能够解析,首先是 Topts
xml以上的字段,但对于 TData
& TCustOpts
字段,我没有得到正确的迭代和方法来解析xml并面临异常。有人可以为我提供 TData
&的正确方法吗? TCustOpts
字段解析。
下面是我的代码供参考。
I am able to parse, first Topts
field of above xml, But for TData
& TCustOpts
field, I am not getting right iteration and approach to parse the xml and facing exception. Can someone provide me right approach for TData
& TCustOpts
field parsing.Below is my code for reference.
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;
}
}
}
}
推荐答案
好吧,使用属性树解析信息时通常使用的反模式是循环狂潮。
Ok, the usual anti-pattern when using Property Tree to parse information is "loop frenzy".
存储密钥的整个想法树格式的值对是为了避免循环低级结构,而不必使用方便的寻址(使用路径)。
The whole idea of storing key-value pairs in a tree format is to avoid having to loop low-level structures, instead using convenient addressing (using paths).
另一种反模式是使所有一个大功能的解析。我会把事情分解。
Another anti-pattern is to have all the parsing in one big function. I'd split things up.
让我们先定义一些数据类型保持我们的数据易于管理:
Let's start with defining some data-types to keep our data manageable:
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;
};
}
这是我们的临时域层。
所以,这就是我编写解析代码的方式:
So, here's how I'd write the parsing code:
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
处理可能不存在的子树。
The only not-so-straight-forward thing is parse_optional
to deal with subtrees that might be absent.
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
}
奖金:往返
我们也将相同的 Domain类保存回属性树,因此我们可以验证其是否有效:
BONUS: Roundtrip
Let's also save the same "Domain" classes back to a property tree, so we can verify it works:
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的解析和序列化:
FULL DEMO
This demo shows your original XML parsed and serialized back:
#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>
这篇关于boost :: property_tree:解析复杂的xml结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!