Progress 4GL:从XML读取数据集

时间:2014-06-30 12:27:07

标签: xml progress-4gl openedge

当我尝试使用Dataset和READ-XML方法从XML读取TempTables时,它可以正常工作。 但问题是如此:如果标签中有属性,则返回空结果而不是元素值。以不同的方式尝试,但没有得到任何结果。

我发现仅当属性名称为“xmlns”时才有效,例如Reading XML file to Dataset in Progress-4gl

这是我的代码和XML。

<?xml version="1.0" encoding="ISO-8859-15"?>
<?xml-stylesheet type="text/xsl" href="Test.xsl"?>
<ReceiverInfo Version="2.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Test.xsd">
   <MessageDetails>
      <MessageTypeCode Axmlns="">RECEIVERINFO</MessageTypeCode>
   </MessageDetails>
   <MessageDetails>
      <MessageTypeCode Axmlns="">RECEIVERINFO2</MessageTypeCode>
   </MessageDetails>
</ReceiverInfo>

代码:

def var icount as int no-undo initial 0.

DEF TEMP-TABLE ReceiverInfo NO-UNDO
   FIELD foo AS LOG.

DEF TEMP-TABLE MessageDetails NO-UNDO
   FIELD MessageTypeCode as CHAR XML-NODE-TYPE "Element".

DEF DATASET data FOR ReceiverInfo, MessageDetails.

DATASET data:READ-XML("file", "/home/ars/temp/tofile.xml", "empty", ?, FALSE, ?, "IGNORE") NO-ERROR.

DO icount = 1 TO ERROR-STATUS:NUM-MESSAGES:
      DISP ERROR-STATUS:GET-MESSAGE(icount) format "x(50)".
END.

FOR EACH MessageDetails.
      DISP MessageDetails.MessageTypeCode format "x(40)".
END.

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

由于该属性是MessageTypeCode的子类,因此MessageTypeCode应该是临时表而不是字段。 ReceiverInfo可以是DATASET。 我更喜欢使用PARENT-ID-RELATION链接父(MessageDetails)和子(MessageTypeCode)临时表。

DEFINE TEMP-TABLE MessageDetails NO-UNDO
    FIELD foo AS LOGICAL.

DEFINE TEMP-TABLE MessageTypeCode NO-UNDO
    FIELD Axmlns               AS CHARACTER XML-NODE-TYPE "Attribute"
    FIELD ElementValue         AS CHARACTER XML-NODE-TYPE "Text"
    FIELD MessageDetails_recid AS RECID     XML-NODE-TYPE "Hidden".

DEFINE DATASET ReceiverInfo
      FOR MessageDetails, MessageTypeCode
      PARENT-ID-RELATION tab1tab2 FOR MessageDetails, MessageTypeCode PARENT-ID-FIELD MessageDetails_recid.

DATASET ReceiverInfo:READ-XML("file", "/home/ars/temp/tofile.xml", "empty", ?, FALSE, ?, "IGNORE") NO-ERROR.

FOR EACH MessageTypeCode:
    DISP MessageTypeCode.ElementValue FORMAT "X(30)"
         MessageTypeCode.Axmlns      
         MessageTypeCode.MessageDetails_recid.
END.

您可以使用XML-NODE-TYPE“Text”来获取MessageTypeCode的值。

注意:每个表只能包含一个TEXT字段。当表包含TEXT字段时,它不能包含ELEMENT字段;它只能包含ATTRIBUTE字段。包含TEXT字段的表不能是嵌套数据关系的一部分。

答案 1 :(得分:0)

要在版本10.x中执行此操作,请执行以下操作:

DEFINE TEMP-TABLE MessageDetails NO-UNDO XML-NODE-NAME "MessageDetails"
    FIELD c_recid               AS RECID XML-NODE-TYPE "Hidden".

DEFINE TEMP-TABLE MessageTypeCode NO-UNDO
    FIELD Axmlns                AS CHARACTER XML-NODE-TYPE "Attribute"
    FIELD ElementValue          AS CHARACTER XML-NODE-TYPE "Text"
    FIELD c_recid               AS RECID     XML-NODE-TYPE "Hidden".

DEFINE DATASET dsReceiverInfo XML-NODE-NAME 'ReceiverInfo'
    FOR  MessageDetails, MessageTypeCode
    DATA-RELATION dr1 FOR MessageDetails, MessageTypeCode 
                      RELATION-FIELDS(c_recid, c_recid) NESTED foreign-key-hidden.

DATASET dsReceiverInfo:READ-XML("file", "/home/ars/temp/tofile.xml", "empty", ?, FALSE, ?, "IGNORE").

FOR EACH MessageTypeCode:
    DISP MessageTypeCode.c_recid
         MessageTypeCode.Axmlns
         MessageTypeCode.ElementValue FORMAT "X(20)"
        WITH STREAM-IO WIDTH 300.
END.