2015-09-29 56 views
0

我正在使用幾個XML文件,每個都有自己的處理程序類。每個類都具有相同但只有一行的loadXML和exportXML函數。我想確定一種方法,我不必在每次爲新XML創建新處理程序類時複製和粘貼。gSoap XML數據綁定 - 可以自動完成讀取/寫入功能?

對於每個文件,我只改變:

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

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

其中GT是命名空間和庫是根節點。每個新的XML文件都會有不同的名稱空間和根節點。這些現在在編譯之前,是否有自動用它們受尊敬的名稱空間和根節點替換每個類load/exportXML函數?

例如我使用命名空間測試和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; 
} 
} 

回答

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)