如何在 OpenEdge Progress4GL 中将 XML 响应转换为 temp-table?

How to convert XML response to temp-table in OpenEdge Progress4GL?

您好 Progress4GL 开发人员,

我正在尝试使用进度来消耗 SOAP API。目前,我正在使用一个名为:http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL.

的开源示例 SOAP 服务

下面的程序实现了一个梦想(即用户输入国家代码 "ESP" 并向用户显示有关西班牙的详细信息)。结果存入temp-table1和temp-table2:

/******* Sample Application to show Progress4GL Consuming a SOAP API *************/

/******* VARIABLES ***************************************************************/
DEFINE VARIABLE lReturn AS LOGICAL NO-UNDO.
DEFINE VARIABLE hServer AS HANDLE NO-UNDO.
DEFINE VARIABLE hPortType AS HANDLE NO-UNDO.
DEFINE VARIABLE capitalCity as LONGCHAR NO-UNDO.

DEFINE VARIABLE iCntryCode as char no-undo label "Country Code".
DEFINE VARIABLE oResponse as LONGCHAR no-undo.

/******* CONNECTION SETTINGS TO SOAP SERVICE *************************************/
CREATE SERVER hServer.
lReturn = hServer:CONNECT("-WSDL http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL"). 

IF lReturn = NO THEN DO:
 MESSAGE
  "Could not connect to WebService server".
 END.

/******* SETTING PORT TYPE *******************************************************/
RUN CountryInfoServiceSoapType SET hPortType ON SERVER hServer.

IF NOT VALID-HANDLE(hPortType) THEN DO:
 MESSAGE
  "Could not establish portType".
 END.

/****** USER UPDATE FORM**********************************************************/
UPDATE iCntryCode. 

/****** NAME OF DATA FUNCTION TO RUN *********************************************/
RUN FullCountryInfo IN hPortType (INPUT iCntryCode, OUTPUT oResponse) NO-ERROR.

/****** TEMP TABLE TO HOLD ALL CAPITAL CITY FIELDS *******************************/
DEFINE TEMP-TABLE temptable1 
SERIALIZE-NAME "sCapitalCity" 
    FIELD capitalCity AS CHAR XML-NODE-TYPE "text". 

/****** DATASET WHICH RUNS CONVERSION ********************************************/
DEFINE DATASET dsa SERIALIZE-NAME "FullCountryInfoResult" FOR temptable1. 

/***** TEMP TABLE TO HOLD ALL CURRENCY FIELDS ************************************/
DEFINE TEMP-TABLE temptable2 
SERIALIZE-NAME "sCurrencyISOCode" 
    FIELD currencyCode AS CHAR XML-NODE-TYPE "text". 

/****** DATASET WHICH RUNS CONVERSION ********************************************/
DEFINE DATASET dsb SERIALIZE-NAME "FullCountryInfoResult" FOR temptable2.

/****** POPULATE DATASET AND TEMP-TABLES FROM SOAP RESPONSE **********************/
DATASET dsa:READ-XML( "longchar", oResponse, ?, ?, ? ). 
DATASET dsb:READ-XML( "longchar", oResponse, ?, ?, ? ).

FOR EACH temptable1 no-lock, each temptable2 no-lock:
  DISPLAY temptable1.capitalcity temptable2.currencyCode.
END.

PAUSE 100.

/****** STOP CONNECTION TO SERVER ************************************************/
DELETE PROCEDURE hPortType.
  hServer:DISCONNECT().
  DELETE OBJECT hServer.

但是,此示例 Web 服务还有一个名为 FullCountryInfoAllCountries 的过程。这将为所有国家做出回应。我试图重构上面的代码,以便所有 temp-table1 和 temp-table2 都将填充所有国家,但是我在解析 XML 数据时遇到困难。

我已经尝试使用 NAMESPACE-URI、SERIALIZATION-NAME 和 XML-NODE-NAME 语句进行试验,但无法使其正常工作。我遇到的问题是,来自 FullCountryInfoAllCountries 的 XML 响应的结构与 FullCountryInfo 不同,我总是会收到类似 'Namespace not found' 的错误,否则它将 return表。

非常感谢!

您可以通过发送响应消息(注释掉)来查看输出,然后可以将其映射到数据集、temp-table 和字段名称:

define variable hs as handle no-undo.
define variable hp as handle no-undo.
define variable lcresponse as longchar no-undo.

define temp-table tt serialize-name "tCountryInfo" 
   field sISOCode as character
   field sName    as character
   .

define dataset ds serialize-name "FullCountryInfoAllCountriesResult" for tt. 

create server hs.
hs:connect( "-WSDL http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL" ). 

run CountryInfoServiceSoapType set hp on server hs.

run FullCountryInfoAllCountries in hp ( output lcresponse ).

// message string( substring( lcresponse, 1, 30000 ).

dataset ds:read-xml( "longchar", lcresponse, ?, ?, ? ). 

for each tt:
  display tt.
end.

delete procedure hp.
hs:disconnect().
delete object hs.