Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore an unknown XML element node in GSOAP

Tags:

c++

gsoap

I am new to GSOAP, so might be missing something obvious here. But I really couldn't find its solution in GSOAP documentations.

I need to know, how do I silently ignore an unknown node in my xml in GSOAP without affecting other nodes.

For example: I have below class

class gsoap_ex
{
int foo;
char bar;    
}

and below XML for it:

<gsoap_ex>
<foo>foo_value</foo>
<unknown>unknown_value</unknown>
<bar>bar_value</bar> 
</gsoap_ex>

As of now, my gsoap parses the xml till it reaches the unknown node, after that it returns without further parsing it.

print_after_parsing(gsoap_ex *obj)
{
cout<<obj->foo;
cout<<obj->bar;
}

So in my above function it shows the value of foo but value of bar is not set.

How do I achieve it?

like image 952
foobar Avatar asked Apr 08 '15 11:04

foobar


1 Answers

You can configure gSOAP to provide a function pointer to deal with unknown elements, and have your function decide what to do with it.

The https://www.cs.fsu.edu/~engelen/soapfaq.html page discusses handling unknown data. The first part of the paragraph is about detecting unexpected data so that you can figure out what to do about the problem; it seems you've already got that covered, so I've just included the sections detailing how to change the behavior of gSOAP.

My code appears to ignore data when receiving SOAP/XML messages. How can I detect unrecognized element tags at run time and let my application fault?

...

Another way to control the dropping of unknown elements is to define the fignore callback. For example:

{ 
    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 
}

The tag parameter contains the offending tag name. You can also selectively return a fault:

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;
}

Presumably, you'd want to write such a callback that returns SOAP_OK for your unexpected data.

like image 79
antiduh Avatar answered Oct 17 '22 09:10

antiduh