struct 互转 xml

前端之家收集整理的这篇文章主要介绍了struct 互转 xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. /** struct2xml.c */
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "soapStub.h"
  6. #include "soapH.h"
  7. #include "nsmap.h"
  8.  
  9. /**
  10. * struct2xml: transfer struct to xml
  11. * strname: the struct name
  12. * strvar: a struct instance name
  13. * xmlbuf: buffer store the result
  14. * buflen: buffer size
  15. */
  16. #define struct2xml(strname,strvar,xmlbuf,buflen) \
  17. do { \
  18. struct soap soap = {0}; \
  19. struct strname *param = &strvar; \
  20. int pfd[2]; \
  21. if (pipe(pfd)) \
  22. break; \
  23. soap_init(&soap); \
  24. soap.sendfd = pfd[1]; \
  25. soap_write_PointerTo##strname(&soap,(struct strname * const*) &param); \
  26. read(pfd[0],buflen - 1); \
  27. close(pfd[0]); \
  28. close(pfd[1]); \
  29. soap_destroy(&soap); \
  30. soap_end(&soap); \
  31. soap_done(&soap); \
  32. } while (0)
  33.  
  34. int main(int argc,char *argv[])
  35. {
  36. struct RequestUpdate req = {0};
  37. snprintf(req.Action,sizeof(req.Action),"%s","Action");
  38. snprintf(req.Auth.Username,sizeof(req.Auth.Username),"Username");
  39. snprintf(req.Auth.Password,sizeof(req.Auth.Password),"Password");
  40. snprintf(req.Auth.Createtm,sizeof(req.Auth.Createtm),"Createtm");
  41. snprintf(req.Auth.Nonce,sizeof(req.Auth.Nonce),"Nonce");
  42. snprintf(req.DevInfo.Product,sizeof(req.DevInfo.Product),"Product");
  43. snprintf(req.DevInfo.Firmware,sizeof(req.DevInfo.Firmware),"Firmware");
  44. snprintf(req.DevInfo.SN,sizeof(req.DevInfo.SN),"SN");
  45.  
  46. char xmldata[2048] = {0};
  47. int xmlsize = sizeof(xmldata);
  48.  
  49. struct2xml(RequestUpdate,req,xmldata,sizeof(xmldata));
  50. printf("%s\n",xmldata);
  51.  
  52. return 0;
  53. }



  1. /** xml2struct.c */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "soapStub.h"
  6. #include "soapH.h"
  7. #include "nsmap.h"
  8.  
  9. /**
  10. * xml2struct: transfer xml to struct
  11. * strname: the struct name
  12. * strvar: a struct instance name
  13. * xmlbuf: xml string
  14. */
  15. #define xml2struct(strname,xmlbuf) \
  16. do { \
  17. struct soap soap; \
  18. int pfd[2]; \
  19. if (pipe(pfd)) \
  20. break; \
  21. write(pfd[1],strlen(xmlbuf)); \
  22. soap_init(&soap); \
  23. soap_begin(&soap); \
  24. soap.recvfd = pfd[0]; \
  25. soap_begin_recv(&soap); \
  26. if (!soap_get_##strname(&soap,&strvar,NULL,NULL)) \
  27. memset(&strvar,sizeof(struct strname)); \
  28. soap_end_recv(&soap); \
  29. soap_destroy(&soap); \
  30. soap_end(&soap); \
  31. soap_done(&soap); \
  32. close(pfd[0]); \
  33. close(pfd[1]); \
  34. } while (0)
  35.  
  36. int main()
  37. {
  38. 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>";
  39.  
  40. struct RequestUpdate req = {0};
  41. xml2struct(RequestUpdate,xmldata);
  42. 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);
  43. return 0;
  44. }

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

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

猜你在找的XML相关文章