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

soap_out_TYPE,soap_put_TYPE


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

两个的区别是put只能输出一次,只能在一个函数中调用一次,out则可以调用多次,根据id的不同实现多个输出
实际上put的实现也是调用了out来实现的!
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,tag,SOAP_TYPE_int);
    if (soap_out_int(soap,tag?tag:"int",id,a,type))
        return soap->error;
    return soap_putindependent(soap);
}


输出整型

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


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


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


soap_out_int(soap,1,NULL);
soap_out_int(soap,2,NULL);


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




输出字符串


char *str="hehe";
soap_serialize_string(soap,&str);
soap_put_string(soap,&str,"mystring",NULL);


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


使用soap_out_string(soap,NULL)函数的输出
soap_out_string(soap,NULL);
soap_out_string(soap,NULL);


页面会输出:
<?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函数实现


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


页面会输出:
<?xml version="1.0" encoding="UTF-8"?>
<logs>
    <content>hehe</content>
    <id>1</id>
</logs>


多次调用soap_out_LogInfo函数
soap_serialize_LogInfo(soap,&loginfo)
soap_out_LogInfo(soap,NULL); 
soap_out_LogInfo(soap,NULL); 


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

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念