当前位置:编程学习 > XML/UML >>

C语言实现gsoap输出数据类型到XML的方法

soap_out_TYPE,soap_put_TYPE

 

soap中输出数据都有两个函数soap_out_TYPE,soap_put_TYPE

    两个的区别是put只能输出一次,只能在一个函数中调用一次,out则可以调用多次,根据id的不同实现多个输出
实际上put的实现也是调用了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);
}
   

输出整型
[cpp]
int num=12345; 
soap_put_int(soap, &num, "myint", NULL); 

int num=12345;
soap_put_int(soap, &num, "myint", NULL);

页面会输出
[html]
?xml version="1.0" encoding="UTF-8"?> 
<myint>12345</myint> 

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

注意:在调用put_int函数时不需要先调用soap_serialize_int(soap, &num);
            而在输出string或者结构体时需要先调用相关的 soap_serialize_type函数。


[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);

函数则会输出
[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>

函数在第一次调用out的时候会先把xml的头输出来,<?xml version="1.0" encoding="UTF-8"?>

 

 

输出字符串

 

[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);

页面会输出
[html]
<?xml version="1.0" encoding="UTF-8"?> 
<mystring>hehe</mystring> 

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

使用soap_out_string(soap, &str , "mystring", NULL)函数的输出
[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);

页面会输出:
[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>

 


输出多个不同类型的参数(结构体)
在gsoap中想要输出多个不同类型的参数只能使用结构体的方法,
自定义一个结构体,在结构体中定义想要的输出的类型和参数,在当然必须在头文件中定义
然后用soapcpp2工具编译此头文件会生成相关的soap_out_TYPE,soap_put_TYPE,soap_serialize_TYPE等函数
实际上这几个函数的实现也是调用了相关类型的函数实现的,比如结构体中包含字符串,那么soap_serialize_TYPE就会调用soap_serialize_string函数实现


如下代码
[cpp]
struct LogInfo loginfo={"哈哈",1}; 
soap_serialize_LogInfo(soap,&loginfo) 
soap_put_LogInfo(soap, &loginfo, "logs", NULL);  

struct LogInfo loginfo={"哈哈",1};
soap_serialize_LogInfo(soap,&loginfo)
soap_put_LogInfo(soap, &loginfo, "logs", NULL);

页面会输出:
[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>

多次调用soap_out_LogInfo函数
[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);

页面会输出
[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>

 

 

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,