程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> C語言實現gsoap輸出數據類型到XML的方法

C語言實現gsoap輸出數據類型到XML的方法

編輯:關於C

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>

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved