当我执行以下代码以进度语言读取XML文件时,该XML文件中仅需要3个项目,但是此代码无法读取它。
代码是:
DEFINE VARIABLE hDoc AS HANDLE NO-UNDO.
DEFINE VARIABLE hRoot AS HANDLE NO-UNDO.
DEFINE VARIABLE hTable AS HANDLE NO-UNDO.
DEFINE VARIABLE hField AS HANDLE NO-UNDO.
DEFINE VARIABLE hText AS HANDLE NO-UNDO.
DEFINE VARIABLE hBuf AS HANDLE NO-UNDO.
DEFINE VARIABLE hDBFld AS HANDLE NO-UNDO.
DEFINE VARIABLE ix AS INTEGER NO-UNDO.
DEFINE VARIABLE jx AS INTEGER NO-UNDO.
DEFINE VARIABLE returnCode AS DECIMAL NO-UNDO.
DEFINE VARIABLE returnMessage AS CHARACTER NO-UNDO.
DEFINE VARIABLE bravoId AS CHAR NO-UNDO.
CREATE X-DOCUMENT hDoc.
CREATE X-NODEREF hRoot.
CREATE X-NODEREF hTable.
CREATE X-NODEREF hField.
CREATE X-NODEREF hText.
/* Read in the file created in i-outcus.p */
hDoc:LOAD("file", "C:\user53.xml", TRUE).
hDoc:GET-DOCUMENT-ELEMENT(hRoot).
REPEAT ix = 1 TO hRoot:NUM-CHILDREN:
hRoot:GET-CHILD(hTable, ix).
/* Get the fields given as attributes */
string(returnCode) = hTable:GET-NODE("returnCode").
returnMessage = hTable:GET-NODE("returnMessage").
bravoId = hTable:GET-NODE("Id").
REPEAT jx = 1 TO hTable:NUM-CHILDREN:
hTable:GET-CHILD(hField, jx).
hField:GET-CHILD(hText, 1).
hDBFld:BUFFER-VALUE = hTEXT:NODE-VALUE.
END.
END.
DELETE OBJECT hDoc.
DELETE OBJECT hRoot.
DELETE OBJECT hTable.
DELETE OBJECT hField.
DELETE OBJECT hText.
FOR EACH v:
DISPLAY ' ReturnCode: ' + STRING(returnCode) + ' - ReturnMessage: ' + returnMessage + ' id: ' + id.
END.
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:ImportProfilesResponse xmlns:ns1="http://host.bs.com/common/webservice/ProfileManagement/">
<returnCode>-996</returnCode>
<returnMessage>Invalid SOAP Request Object - 112(FISCAL_CODE);</returnMessage>
<id>0000</id>
</ns1:ImportProfilesResponse>
</soapenv:Body>
</soapenv:Envelope>
答案 0 :(得分:3)
有多种方法可以执行此操作。也许您可以使用临时表定义数据集,然后简单地将数据读入其中。
这是我最后得到的解决方案的一部分。也可以递归地完成(遍历xml)。
基本上,您需要做的是遍历xml,当您击中正确的节点时,您又下一级了。等等。此外,您还需要了解节点的值实际上就是它本身的一个节点。
DEFINE VARIABLE hDoc AS HANDLE NO-UNDO.
DEFINE VARIABLE hRoot AS HANDLE NO-UNDO.
DEFINE VARIABLE hTable AS HANDLE NO-UNDO.
DEFINE VARIABLE hField AS HANDLE NO-UNDO.
DEFINE VARIABLE hText AS HANDLE NO-UNDO.
DEFINE VARIABLE hValue AS HANDLE NO-UNDO.
DEFINE VARIABLE ix AS INTEGER NO-UNDO.
DEFINE VARIABLE jx AS INTEGER NO-UNDO.
DEFINE VARIABLE kx AS INTEGER NO-UNDO.
DEFINE VARIABLE returnCode AS DECIMAL NO-UNDO.
DEFINE VARIABLE returnMessage AS CHARACTER NO-UNDO.
DEFINE VARIABLE bravoId AS CHAR NO-UNDO.
CREATE X-DOCUMENT hDoc.
CREATE X-NODEREF hRoot.
CREATE X-NODEREF hTable.
CREATE X-NODEREF hField.
CREATE X-NODEREF hText.
CREATE X-NODEREF hValue.
/* Read in the file created in i-outcus.p */
hDoc:LOAD("file", "C:\temp\user53.xml", FALSE).
hDoc:GET-DOCUMENT-ELEMENT(hRoot).
REPEAT ix = 1 TO hRoot:NUM-CHILDREN:
hRoot:GET-CHILD(hTable, ix).
IF hTable:NAME = "soapEnv:body" THEN DO:
REPEAT jx = 1 TO hTable:NUM-CHILDREN:
hTable:GET-CHILD(hField, jx).
IF hField:NAME = "ns1:ImportProfilesResponse" THEN DO:
REPEAT kx = 1 TO hFIeld:NUM-CHILDREN:
hField:GET-CHILD(hText, kx).
IF hText:NUM-CHILDREN = 1 THEN DO:
hText:GET-CHILD(hValue, 1).
IF hText:NAME = "returnCode" THEN DO:
returnCode = DECIMAL(hValue:NODE-VALUE) NO-ERROR.
END.
ELSE IF hText:NAME = "returnMessage" THEN DO:
returnMessage = hValue:NODE-VALUE.
END.
ELSE IF hText:NAME = "id" THEN DO:
bravoId = hValue:NODE-VALUE.
END.
END.
END.
END.
END.
END.
END.
DELETE OBJECT hDoc.
DELETE OBJECT hRoot.
DELETE OBJECT hTable.
DELETE OBJECT hField.
DELETE OBJECT hText.
DELETE OBJECT hValue.
MESSAGE ' ReturnCode: ' STRING(returnCode) SKIP
' ReturnMessage: ' returnMessage SKIP
' Id: ' bravoId VIEW-AS ALERT-BOX .
答案 1 :(得分:2)