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

gSoap XML data binding - Possible to auto complete read/write functions?

本文关键字:功能 数据绑定 XML      更新时间:2023-10-16

我使用了几个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文件都有不同的名称空间和根节点。这些现在在编译之前,是否有任何方式来自动替换每个类加载/导出xml函数与他们尊重的名称空间和根节点?

。我用名称空间test和根节点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;
 }  
}

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

#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)