How to output data types to XML using gsoap in C Language

Source: Internet
Author: User

Soap_out_TYPE, soap_put_TYPE

 

The output data in soap has two functions: soap_out_TYPE and soap_put_TYPE.

The difference between the two is that put can only be output once and can only be called once in one function. out can be called multiple times to implement multiple outputs based on different IDs.
In fact, the put implementation is also implemented by calling out!
[Cpp]
SOAP_FMAC3 int SOAP_FMAC4 soap_put_int (struct soap * soap, const int * a, const char * tag, const char * type)
{
Register int id = soap_embed (soap, (void *) a, NULL, 0, tag, SOAP_TYPE_int );
If (soap_out_int (soap, tag? Tag: "int", id, a, type ))
Return soap-> error;
Return soap_putindependent (soap );
}

SOAP_FMAC3 int SOAP_FMAC4 soap_put_int (struct soap * soap, const int * a, const char * tag, const char * type)
{
Register int id = soap_embed (soap, (void *) a, NULL, 0, tag, SOAP_TYPE_int );
If (soap_out_int (soap, tag? Tag: "int", id, a, type ))
Return soap-> error;
Return soap_putindependent (soap );
}

Output integer
[Cpp]
Int num = 12345;
Soap_put_int (soap, & num, "myint", NULL );

Int num = 12345;
Soap_put_int (soap, & num, "myint", NULL );

Page output
[Html]
? Xml version = "1.0" encoding = "UTF-8"?>
<Myint> 12345 </myint>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Myint> 12345 </myint>

Note: you do not need to call soap_serialize_int (soap, & num) before calling the put_int function );
When outputting a string or struct, you must first call the related soap_serialize_type function.


[Cpp]
Soap_out_int (soap, "myint", 1, & num, NULL );
Soap_out_int (soap, "myint", 2, & num, NULL );

Soap_out_int (soap, "myint", 1, & num, NULL );
Soap_out_int (soap, "myint", 2, & num, NULL );

The function outputs
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Myint id = "_ 1"> 12345 </myint> <myint id = "_ 2"> 12345 </myint>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Myint id = "_ 1"> 12345 </myint> <myint id = "_ 2"> 12345 </myint>

When a function calls out for the first time, it first outputs the xml header. <? Xml version = "1.0" encoding = "UTF-8"?>

 

 

Output string

 

[Cpp]
Char * str = "hehe ";
Soap_serialize_string (soap, & str );
Soap_put_string (soap, & str, "mystring", NULL );

Char * str = "hehe ";
Soap_serialize_string (soap, & str );
Soap_put_string (soap, & str, "mystring", NULL );

Page output
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Mystring> hehe </mystring>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Mystring> hehe </mystring>

Output Using the soap_out_string (soap, & str, "mystring", NULL) Function
[Cpp]
Soap_out_string (soap, "mystring", 1, & str, NULL );
Soap_out_string (soap, "mystring", 2, & str, NULL );

Soap_out_string (soap, "mystring", 1, & str, NULL );
Soap_out_string (soap, "mystring", 2, & str, NULL );

The page will output:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Mystring id = "_ 1"> hehe </mystring> <mystring id = "_ 2"> hehe </mystring>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Mystring id = "_ 1"> hehe </mystring> <mystring id = "_ 2"> hehe </mystring>

 


Output multiple parameters of different types (struct)
To output multiple parameters of different types in gsoap, you can only use struct methods,
Customize a struct to define the type and parameters of the desired output in the struct. Of course, the struct must be defined in the header file.
Then, compile the header file with soapcpp2 to generate related functions such as soap_out_TYPE, soap_put_TYPE, and soap_serialize_TYPE.
In fact, the implementation of these functions also calls functions of the relevant types. For example, if the struct contains strings, soap_serialize_TYPE will call the soap_serialize_string function implementation.


The following code
[Cpp]
Struct LogInfo loginfo = {"Haha", 1 };
Soap_serialize_LogInfo (soap, & loginfo)
Soap_put_LogInfo (soap, & loginfo, "logs", NULL );

Struct LogInfo loginfo = {"Haha", 1 };
Soap_serialize_LogInfo (soap, & loginfo)
Soap_put_LogInfo (soap, & loginfo, "logs", NULL );

The page will output:
[Html]
? Xml version = "1.0" encoding = "UTF-8"?>
<Logs>
<Content> hehe </content>
<Id> 1 </id>
</Logs>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Logs>
<Content> hehe </content>
<Id> 1 </id>
</Logs>

Multiple calls to the soap_out_LogInfo Function
[Cpp]
Soap_serialize_LogInfo (soap, & loginfo)
Soap_out_LogInfo (soap, "logs", 1, & loginfo, NULL );
Soap_out_LogInfo (soap, "logs", 2, & loginfo, NULL );

Soap_serialize_LogInfo (soap, & loginfo)
Soap_out_LogInfo (soap, "logs", 1, & loginfo, NULL );
Soap_out_LogInfo (soap, "logs", 2, & loginfo, NULL );

Page output
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Logs id = "_ 1">
<Content> hehe </content>
<Id> 1 </id>
</Logs>
<Logs id = "_ 2">
<Content> hehe </content>
<Id> 1 </id>
</Logs>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Logs id = "_ 1">
<Content> hehe </content>
<Id> 1 </id>
</Logs>
<Logs id = "_ 2">
<Content> hehe </content>
<Id> 1 </id>
</Logs>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.