忽略GSOAP中的未知XML元素节点

foo*_*bar 7 c++ gsoap

我是GSOAP的新手,所以可能会遗漏一些明显的东西.但我真的无法在GSOAP文档中找到它的解决方案.

我需要知道,如何在GSOAP中静默忽略我的xml中的未知节点而不影响其他节点.

例如:我有下课

class gsoap_ex
{
int foo;
char bar;    
}
Run Code Online (Sandbox Code Playgroud)

以下XML:

<gsoap_ex>
<foo>foo_value</foo>
<unknown>unknown_value</unknown>
<bar>bar_value</bar> 
</gsoap_ex>
Run Code Online (Sandbox Code Playgroud)

截至目前,我的gsoap解析xml直到它到达未知节点,之后它返回而不进一步解析它.

print_after_parsing(gsoap_ex *obj)
{
cout<<obj->foo;
cout<<obj->bar;
}
Run Code Online (Sandbox Code Playgroud)

所以在我的上面的函数中它显示了foo的值,但是没有设置bar的值.

我该如何实现?

ant*_*duh 2

您可以配置 gSOAP 以提供函数指针来处理未知元素,并让您的函数决定如何处理它。

https://www.cs.fsu.edu/~engelen/soapfaq.html页面讨论处理未知数据。该段落的第一部分是关于检测意外数据,以便您可以找出解决问题的方法;看来您已经了解了这一点,所以我刚刚包含了详细说明如何更改 gSOAP 行为的部分。

我的代码在接收 SOAP/XML 消息时似乎忽略数据。如何在运行时检测无法识别的元素标签并让我的应用程序出现故障?

...

控制未知元素删除的另一种方法是定义 Fignore 回调。例如:

{ 
    struct soap soap;   
    soap_init(&soap);   
    soap.fignore = mustmatch; // overwrite default callback   
    ...
    soap_done(&soap); // reset callbacks 
} 

int mustmatch(struct soap *soap, const char *tag) { 
    return SOAP_TAG_MISMATCH; // every tag must be handled 
}
Run Code Online (Sandbox Code Playgroud)

tag 参数包含有问题的标签名称。您还可以有选择地返回错误:

int mustmatch(struct soap *soap, const char *tag) { 

    // all tags in namespace "ns" that start with "login" are optional
    if (soap_match_tag(soap, tag, "ns:login*"))   
        return SOAP_OK;   

    // every other tag must be understood (handled)
    return SOAP_TAG_MISMATCH;
}
Run Code Online (Sandbox Code Playgroud)

据推测,您希望编写这样一个回调SOAP_OK来返回意外的数据。