如何在boost中解析属性?Soap xml解析
How are attributes parsed in boost? Soap xml parsing?
我正在尝试解码下面给出的一个soap xml数据包
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope>
<SOAP-ENV:Header><cwmp:ID SOAP-ENV:mustUnderstand="230">105</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<cwmp:Request>
<Id>100</Id>
<RequestEvent SOAP-ENC:arrayType="cwmp:RequestStruct[3]">
<RequestStruct>
<Code>0 ZERO</Code>
<Key></Key>
</RequestStruct>
<RequestStruct>
<Code>1 ONE</Code>
<Key></Key>
</RequestStruct>
<RequestStruct>
<Code>2 TRAP</Code>
<Key></Key>
</RequestStruct>
</RequestEvent>
</cwmp:Request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
当我试图使用代码解码数据包时
BOOST_FOREACH(boost::property_tree::ptree::value_type &SL_vTemp, pt.get_child("SOAP-ENV:Envelope.SOAP-ENV:Body.cwmp:Request.RequestEvent"))
{
struct param obj;
obj._Name.assign(SL_vTemp.second.get<std::string>("Code"));
obj._Value.assign(SL_vTemp.second.get<std::string>("Key"))
}
我得到的异常是没有名为EventCode的节点。但是,如果我正在从SOAP xml数据包中删除属性部分"SOAP-ENC:arrayType="cwmp:RequestStruct[3]",那么代码工作正常。提前谢谢。
您应该用一个条件包围块,以检查它是否是RequestStruct
元素:
if (SL_vTemp.first == "RequestStruct")
{
auto code = SL_vTemp.second.get<std::string>("Code");
auto key = SL_vTemp.second.get<std::string>("Key");
std::cout << "code:" << code << " key:" << key << "n";
}
因为属性在<xmlattr>
前缀下。
在Coliru上直播
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <iostream>
extern std::string const sample;
int main() {
boost::property_tree::ptree pt;
{
std::istringstream iss(sample);
read_xml(iss, pt);
}
BOOST_FOREACH (boost::property_tree::ptree::value_type &SL_vTemp,
pt.get_child("SOAP-ENV:Envelope.SOAP-ENV:Body.cwmp:Request.RequestEvent"))
{
if (SL_vTemp.first == "RequestStruct")
{
auto code = SL_vTemp.second.get<std::string>("Code");
auto key = SL_vTemp.second.get<std::string>("Key");
std::cout << "code:" << code << " key:" << key << "n";
} else
{
std::cout << "skipped: '" << SL_vTemp.first << "'n";
}
}
}
std::string const sample = R"(
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope>
<SOAP-ENV:Header><cwmp:ID SOAP-ENV:mustUnderstand="230">105</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<cwmp:Request>
<Id>100</Id>
<RequestEvent SOAP-ENC:arrayType="cwmp:RequestStruct[3]">
<RequestStruct>
<Code>0 ZERO</Code>
<Key>key0</Key>
</RequestStruct>
<RequestStruct>
<Code>1 ONE</Code>
<Key>key1</Key>
</RequestStruct>
<RequestStruct>
<Code>2 TRAP</Code>
<Key>key2</Key>
</RequestStruct>
</RequestEvent>
</cwmp:Request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
)";
打印
skipped: '<xmlattr>'
code:0 ZERO key:key0
code:1 ONE key:key1
code:2 TRAP key:key2
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- 使用 mod_gsoap 部署服务时,如何在 Gsoap 中更改 soap 上下文的模式?
- 按多个属性排序
- 主函数参数的属性
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- C++删除未使用的类属性会导致 std::logic_error
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 使用内存地址访问结构的属性值
- C++调用具有 *this 属性的单个帮助程序函数
- C++ 在堆栈中包含多态属性的类对象存储
- 如何在boost中解析属性?Soap xml解析