【问题标题】:gSoap XML data binding - Possible to auto complete read/write functions?gSoap XML 数据绑定 - 可以自动完成读/写功能吗?
【发布时间】:2015-09-29 14:43:59
【问题描述】:

我正在使用几个 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 和 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;
 }  
}

【问题讨论】:

    标签: c++ xml binding gsoap


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多