gSoap XML数据绑定 - 可以自动完成读/写功能吗?

时间:2015-09-29 14:43:59

标签: c++ xml binding gsoap

我使用多个XML文件,每个文件都有自己的处理程序类。每个类都有loadXML和exportXML函数,它们相同但只有一行。我想确定一种方法,每次为新XML创建新的处理程序类时,我都不必复制和粘贴。

对于每个文件,我只是在改变:

if(soap_read__gt__Library(&soap, &library) != SOAP_OK)

if(soap_write__gt__Library(&soap, &library) != SOAP_OK) 

其中gt是命名空间,Library是根节点。每个新的XML文件都有不同的命名空间和根节点。现在这些都是在编译之前,无论如何都要自动将每个类的load / exportXML函数替换为它们受尊重的命名空间和根节点?

e.g。我用命名空间测试和rootnode devConfig创建了一个新的xml。我想要一个用soap_read__test__devConfig和soap_write_test__devConfig替换load / exportXML的方法。

void LoadXML(struct soap& soap, _gt__Library& library, const string& strXMLPath)
{
 ifstream fstreamIN(strXMLPath);
 soap.is = &fstreamIN;   

 // calls soap_begin_recv, soap_get__gt__Library and soap_end_recv
 if(soap_read__gt__Library(&soap, &library) != SOAP_OK)
 {
  std::cout << "soap_read__gt__Library() failed" << std::endl;
  throw 1;
 }

 // patch  
 if(_setmode(_fileno(stdin), _O_TEXT) == -1)
 {
  std::cout << "_setmode() failed" << std::endl;
  throw 1;
 }
 // ~patch  
}

void exportXML(struct soap& soap, _gt__Library& library, const string& strXMLPath)
{
 soap_set_omode(&soap, SOAP_XML_INDENT); 

 ofstream fstreamOUT(strXMLPath);
 soap.os = &fstreamOUT;

 // calls soap_begin_send, soap_serialize, soap_put and soap_end_send
 if(soap_write__gt__Library(&soap, &library) != SOAP_OK) 
 {
  std::cout << "soap_write__gt__Library() failed" << std::endl;      
  throw 1;
 }  
}

1 个答案:

答案 0 :(得分:0)

也许不是最干净的解决方案,但我想你可以使用宏,就像这样:

#define loadXML(soap, gt_name, library, namespaces, root, strXMLPath) \
  ifstream fstreamIN(strXMLPath); \
  soap.is = &fstreamIN; \
  soap_set_namespaces(soap, namespaces); // namespace table \   
  if(soap_begin_recv(soap) || \
    soap_get_##gt_name(soap, library, root, NULL)) || \
    soap_end_recv(soap)) \
  { \
   std::cout << "soap_read__gt__Library() failed" << std::endl; \
   throw 1; \
  } \
  etc.

并展开它以实现您想要的操作:

loadXML(&soap, gt__library, &library, namespaces, "some-root", strXMLPath)