struct 互转 xml

前端之家收集整理的这篇文章主要介绍了struct 互转 xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/** struct2xml.c */

#include <stdio.h>
#include <unistd.h>
#include "soapStub.h"
#include "soapH.h"
#include "nsmap.h"

/**
 * struct2xml: transfer struct to xml 
 * strname: the struct name
 * strvar: a struct instance name
 * xmlbuf: buffer store the result
 * buflen: buffer size
 */
#define struct2xml(strname,strvar,xmlbuf,buflen)	\
	do {						\
		struct soap soap = {0};			\
		struct strname *param = &strvar;	\
		int pfd[2];				\
		if (pipe(pfd))				\
			break;				\
		soap_init(&soap);			\
		soap.sendfd = pfd[1];			\
		soap_write_PointerTo##strname(&soap,(struct strname * const*) &param); \
		read(pfd[0],buflen - 1);	\
		close(pfd[0]);				\
		close(pfd[1]);				\
		soap_destroy(&soap);			\
		soap_end(&soap);			\
		soap_done(&soap);			\
	} while (0)

int main(int argc,char *argv[])
{
	struct RequestUpdate req = {0};
	snprintf(req.Action,sizeof(req.Action),"%s","Action");
	snprintf(req.Auth.Username,sizeof(req.Auth.Username),"Username");
	snprintf(req.Auth.Password,sizeof(req.Auth.Password),"Password");
	snprintf(req.Auth.Createtm,sizeof(req.Auth.Createtm),"Createtm");
	snprintf(req.Auth.Nonce,sizeof(req.Auth.Nonce),"Nonce");
	snprintf(req.DevInfo.Product,sizeof(req.DevInfo.Product),"Product");
	snprintf(req.DevInfo.Firmware,sizeof(req.DevInfo.Firmware),"Firmware");
	snprintf(req.DevInfo.SN,sizeof(req.DevInfo.SN),"SN");

	char xmldata[2048] = {0};
	int xmlsize = sizeof(xmldata);

	struct2xml(RequestUpdate,req,xmldata,sizeof(xmldata));
	printf("%s\n",xmldata);

	return 0;
}



/** xml2struct.c */

#include <stdio.h>
#include <string.h>
#include "soapStub.h"
#include "soapH.h"
#include "nsmap.h"

/**
 * xml2struct: transfer xml to struct
 * strname: the struct name
 * strvar: a struct instance name
 * xmlbuf: xml string 
 */
#define xml2struct(strname,xmlbuf)				\
	do {								\
		struct soap soap;					\
		int pfd[2];						\
		if (pipe(pfd))						\
			break;						\
		write(pfd[1],strlen(xmlbuf));			\
		soap_init(&soap);					\
		soap_begin(&soap);					\
		soap.recvfd = pfd[0];					\
		soap_begin_recv(&soap);					\
		if (!soap_get_##strname(&soap,&strvar,NULL,NULL))	\
			memset(&strvar,sizeof(struct strname));	\
		soap_end_recv(&soap);					\
		soap_destroy(&soap);					\
		soap_end(&soap);					\
		soap_done(&soap);					\
		close(pfd[0]);						\
		close(pfd[1]);						\
	} while (0)

int main()
{
	char *xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<RequestUpdate><Action>Action</Action><Auth><Username>Username</Username><Password>Password</Password><Createtm>Createtm</Createtm><Nonce>Nonce</Nonce></Auth><DevInfo><Product>Product</Product><Firmware>Firmware</Firmware><SN>SN</SN></DevInfo></RequestUpdate>";

	struct RequestUpdate req = {0};
	xml2struct(RequestUpdate,xmldata);
	
	printf("%s,%s,%s\n",req.Action,req.Auth.Username,req.Auth.Password,req.Auth.Createtm,req.Auth.Nonce,req.DevInfo.Product,req.DevInfo.Firmware,req.DevInfo.SN);
	return 0;
}

如果这段代码对你有用, 请帮我顶一个赞

完整代码查看: https://github.com/willalways/struct-xml

原文链接:https://www.f2er.com/xml/295469.html

猜你在找的XML相关文章