ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

基于imx6 的gsoap的简单使用(包含libxml2,libiconv的简单使用)

2019-06-06 15:54:37  阅读:251  来源: 互联网

标签:info sys name libxml2 value libiconv pThird gsoap soap


本文介绍了gsoap的简单使用 包含xml的解析 ,编码格式的转换等:

soap 获取服务器数据:


int get_info( server_info *sys_info, struct _tempuri__queryObjectOut *queryObject,struct _tempuri__queryObjectOutResponse *result_res)
{
    char *result;
    char server[MAX_PATH];
    struct soap soap; 
	char *temp;
	char param[MAX_PATH];
	char str[20];

	static int count = 0;
	free_queryObject(queryObject);
	strncpy(str,sys_info->sn,10);  // 去掉换行
	// 填写上传的xml 请求参数  
	strcpy  (param,"<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n");
	strcat  (param,"<root>\r\n");
	strcat  (param,"   <QueryCondition>\r\n");
	strcat  (param,"       <SN>");
	//strcat  (param,str);
	strcat  (param,"123456");	
	strcat  (param,"</SN>\r\n");		
	strcat  (param,"   </QueryCondition>\r\n");	
	strcat  (param,"</root>");		
	
    queryObject->xtlb      	= strdup("xx");
    queryObject->jkxlh    	= strdup("x'x'x");
    queryObject->jkid 		= strdup("x'x'x");
	queryObject->xmlDoc 	= strdup(param);

	//fprintf(stderr, "queryobjecet---param:%s\n",param);
    soap_init1(&soap,SOAP_C_UTFSTRING);//SOAP_C_UTFSTRINGSOAP_XML_INDENT // soap init
    if (soap.error) 
	{
		printf("soap error!!! \n");
        soap_print_fault(&soap, stderr);
        //exit(1);					// 
		return 1;     //  saop 初始化错误
    }
	
	//fprintf(stderr, "queryobjecet:%s\n",queryObject->xmlDoc);
	//fprintf(stderr, "queryobjecet:%s\n",queryObject->jkxlh);
	//only for test
	sys_info->server_port = 8089;
	snprintf(sys_info->server_ip, MAX_PATH, "10.0.5.46");
	snprintf(server, MAX_PATH, "http://%s:%d/TmriOutAccess.svc?singleWsdl",sys_info->server_ip, sys_info->server_port);
	//fprintf(stderr, "queryobjecet--server:%s\n",server);

    soap_call___tempuri__queryObjectOut(&soap,
										server,
										"http://tempuri.org/ITmriOutAccess/queryObjectOut",
										queryObject,
										result_res
										);

    if (soap.error) 
	{
		printf("soap error after send !!! \n");
        soap_print_fault(&soap, stderr);
        //exit(1);
        soap_destroy(&soap);
    	soap_end(&soap);
    	soap_done(&soap);
		free_queryObject(queryObject);
        return 2; // soap 执行返回错误 
    }   
	fprintf(stderr, "test loop times = %d\n",count++);
	//fprintf(stdout,"soap result %s\n",result_res->queryObjectOutResult);
	soap_destroy(&soap);
    soap_end(&soap);
    soap_done(&soap);
	int return_value = message_handle_query(sys_info,result_res);
	fprintf(stderr, "test loop  return values  = %d\n",return_value);
	switch(return_value)  //  返回信息处理,提取相关信息
	{
		case 0: break;

		default: ;
	}
    return 0;			// 无错误 返回 0 
}

xml 解析和 格式转换程序:

int message_handle_query(server_info *sys_info,struct _tempuri__queryObjectOutResponse *result_res)
{
	int nXmlSize = 0; 
    xmlDocPtr pstXmlDoc ;
    xmlNodePtr pstXmlNode = NULL;
    xmlNodePtr pstXmlSubNode = NULL;
    char *pszXmlNodeContent = NULL;
	char paramt[MAX_PATH];
	size_t nread;
	size_t nconv;
	size_t  avail = 0;
	size_t insize = 0; 
	char dst[2048] = {0};		//
	
	if(strlen(result_res->queryObjectOutResult)<200)  // 长度不够,认为数据返回错误  
	{
		return 1; // 
	}
	
	char *outbuf = dst;
	char *in = result_res->queryObjectOutResult;
	char *out = outbuf;
	
	size_t dst_len = sizeof(dst);
	size_t src_len = strlen(result_res->queryObjectOutResult);
	iconv_t conv = iconv_open ("utf-8", "gbk");    //格式转换
	if(conv == (iconv_t) -1)
	{
		fprintf(stdout,"conv  error !!!");
		return 2;
	}
	 nconv = iconv(conv,&in,&src_len,&out,&dst_len);
		
	if (nconv == (size_t) -1)
	{
		if (errno == EINVAL)
		{
		  fprintf(stdout,"EINVAL\n");
		}
		else
		{
			fprintf(stdout,"error\n");
		}
		return 3;
	}
	//printf("%s: \n",dst);
	iconv_close (conv);
	//printf("%d: retertret xml\n",nXmlSize);
	nXmlSize = sizeof(dst);
	pstXmlDoc = xmlReadMemory(dst, nXmlSize,NULL,"utf-8",XML_PARSE_RECOVER);
	if(pstXmlDoc == NULL)
	{
		fprintf(stdout,"%s_%d: xmlParseMemory failed\n",result_res->queryObjectOutResult,nXmlSize);
		return 4;
	}
	xmlNodePtr root = xmlDocGetRootElement(pstXmlDoc);
	xmlNodePtr curr = root;
	//success_t++;
	
    //fprintf(stdout,"Node name is %s!\n", root->name);

	xmlNodePtr pFirst = root->xmlChildrenNode;// 获取字节点
	while(NULL != pFirst)
	{
		if(!xmlStrcmp(pFirst->name, (const xmlChar *)("head")))
		{
			xmlNodePtr pSecond = pFirst->xmlChildrenNode;
			while(NULL != pSecond)
			{
				xmlChar* value= NULL;

				if(!xmlStrcmp(pSecond->name, (const xmlChar *)("code")))
				{
					value = xmlNodeGetContent(pSecond);
					//printf("\n%s-->%s\n", pSecond->name, value);
					xmlFree(value);
					value = NULL;
				}
				if(!xmlStrcmp(pSecond->name, (const xmlChar *)("message")))
				{
					value = xmlNodeGetContent(pSecond);
					//printf("\n%s-->%s\n", pSecond->name, value);
					xmlFree(value);
					value = NULL;
				}
				if(!xmlStrcmp(pSecond->name, (const xmlChar *)("rownum")))
				{
					value = xmlNodeGetContent(pSecond);
					//printf("\n%s-->%s\n", pSecond->name, value);
					xmlFree(value);
					value = NULL;
				}
				pSecond = pSecond->next;
			}
		}
		if(!xmlStrcmp(pFirst->name, (const xmlChar *)("body")))
		{
			xmlNodePtr pSecond = pFirst->xmlChildrenNode;
			while(NULL != pSecond)
			{
				if(!xmlStrcmp(pSecond->name, (const xmlChar *)("drvexam")))
				{
					xmlNodePtr pThird = pSecond->xmlChildrenNode;
					while(NULL != pThird)
					{
						xmlChar* value= NULL;
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("SN")))
						{
							value = xmlNodeGetContent(pThird);
							//printf("the size of value = %d \n",sizeof(value));
							snprintf(sys_info->sn, MAX_PATH,"%s",value);
							//printf("\n%s-->%s\n", pThird->name, sys_info->sn);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("CLXH")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->clxh, MAX_PATH,"%s",value );
							//printf("\n%s-->%s\n", pThird->name, sys_info->clxh);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("ZDIP")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->server_ip, MAX_PATH,"%s",value );
							//printf("\n%s-->%s\n", pThird->name, sys_info->server_ip);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("SCDK")))
						{
							value = xmlNodeGetContent(pThird);
							sys_info->server_port  = atoi(value);
							//printf("\n%s-->%d\n", pThird->name, sys_info->server_port);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("KCHP")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->car_num, MAX_PATH,"%s",value );
							//printf("\n%s-->%s\n", pThird->name, sys_info->car_num);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("KSDD")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->exam_location, MAX_PATH,"%s",value );
							//printf("\n%s-->%s\n", pThird->name, sys_info->exam_location);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("KCXH")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->kcxh, MAX_PATH,"%s",value );
							//printf("\n%s-->%s\n", pThird->name, sys_info->kcxh);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("KSKM")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->kskm, MAX_PATH,"%s",value );
							//printf("\n%s-->%s\n", pThird->name, sys_info->kskm);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("WG")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->gateway, MAX_PATH,"%s",value );
							//printf("\n%s-->%s\n", pThird->name, sys_info->gateway);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("ZWYM")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->netmask, MAX_PATH,"%s",value );
							//printf("\n%s-->%s\n", pThird->name, sys_info->netmask);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("XTXLH")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->xtxlh, MAX_PATH,"%s",value );
							printf("\n%s-->%s\n", pThird->name, sys_info->xtxlh);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("XHPZ")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->xhpz, MAX_PATH,"%s",value );
							printf("\n%s-->%s\n", pThird->name, sys_info->xhpz);
							xmlFree(value);
							value = NULL;
						}
						if(!xmlStrcmp(pThird->name, (const xmlChar *)("SXSJ")))
						{
							value = xmlNodeGetContent(pThird);
							snprintf(sys_info->sxsj, MAX_PATH,"%s",value );
							printf("\n%s-->%s\n", pThird->name, sys_info->sxsj);
							xmlFree(value);
							value = NULL;
						}
						pThird = pThird->next;
					}
				}
				pSecond = pSecond->next;
			}
		}
		pFirst = pFirst->next;
	}
	xmlFreeDoc(pstXmlDoc);
	return 0;
}

本文介绍了,soap的简单使用以及xml文件的解析,格式转换等使用方法,还有比较麻烦的makefile 后面有时间再写。
关于 libxml2 和 libiconv的交叉编译需要自己找博客编译,简单思路就是安装交叉编译器->解压库文件->根据需要配置config文件->执行配置(设置编译后的文件路径)->编译&安装->复制到开发板的相关目录。如果读者有什么需要和建议欢迎留言交流。

标签:info,sys,name,libxml2,value,libiconv,pThird,gsoap,soap
来源: https://blog.csdn.net/Nyiragongo/article/details/91044798

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有