0

考虑以下我收到并解析的 XML 文档:

<Courses>
  <Course>
    <Name>Intro to DB</Name>
    <Credits>3.0</Credits>
  </Course>
  <Course>
    <Name>Intro to Programming</Name>
    <Credits>3.0</Credits>
  </Course>
</Courses>

如果我想在两门课程之间添加一门新课程或在每门课程中添加一个新元素(例如 courseId)。我怎样才能在进行中实现这一目标?我已经阅读了 Progress 的 XML 文档(针对 DOM 和 SAX)并找到了 INSERT-BEFORE(),不确定是否可以使用它来实现这一点。

我必须使用 temp-table/ProDataSets 来实现这一点吗?

4

1 回答 1

0

带有临时表和数据集的示例

此处的数据集仅用于格式化 xml 输出

DEFINE TEMP-TABLE ttCourse NO-UNDO XML-NODE-NAME "Course"
   FIELD dSORT AS DECIMAL SERIALIZE-HIDDEN // hidden in xml output
   FIELD Name AS CHARACTER
   FIELD Credits AS DECIMAL
   FIELD NEWFIELD AS CHARACTER
                               
   INDEX SORT dSORT ASCENDING. // this index for sorting the xml output Course
                                 

DEFINE VARIABLE cxml AS LONGCHAR  NO-UNDO.
DEFINE VARIABLE iCnt AS INTEGER    NO-UNDO.

// DEFINE DATASET only for xml-node-name
DEFINE DATASET ds 
   XML-NODE-NAME "Courses"  FOR ttCourse.

cxml = "<Courses>
  <Course>
    <Name>Intro to DB</Name>
    <Credits>3.0</Credits>
  </Course>
  <Course>
    <Name>Intro to Programming</Name>
    <Credits>3.0</Credits>
  </Course>
</Courses>".

TEMP-TABLE ttCourse:READ-XML ("LONGCHAR", cxml, "MERGE",  "", FALSE, ?, ?). 

// for sorting output use field dsort
FOR EACH ttCourse WHERE ttCourse.DSORT = 0:
    iCnt = iCnt + 1.
    ttCourse.DSORT = iCnt.
END.

// insert a record in the middle
CREATE ttCourse.
ASSIGN
   ttCourse.DSORT = 1.5
   ttCourse.name = "test"
   ttCourse.credits = 5.5
   ttCourse.NEWFIELD = "NEWFIELD"
   .
  
DATASET ds:WRITE-XML ("file", "C:/temp/baz/courses.xml", TRUE,?,?,FALSE,FALSE).
于 2021-10-14T08:45:53.930 回答