【问题标题】:While processing XML, how to insert a node after a specific node?在处理 XML 时,如何在特定节点之后插入一个节点?
【发布时间】:2021-12-02 10:19:09
【问题描述】:

考虑以下我收到并解析的 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 来实现这一点吗?

【问题讨论】:

  • 临时表或数据集的使用取决于你想做什么。这是您自己创建的 xml,还是以其他方式获得的?您可以使用 DOM 或数据集来完成。 SAX 只会让您解析 xml,而不是创建它。你也可以用文本文件输出来做到这一点,但这可能是一种奇怪的方法(但很简单)。
  • @Jensd 所以 SAX-WRITER 不是真的在写作?!? ;-)
  • @StefanDrissen 当然你是对的,我只是短暂地使用过 SAX-READER。我以为它只是 Windows ......所以我真的从来没有打扰过。

标签: openedge progress-4gl


【解决方案1】:

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

此处的数据集仅用于格式化 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).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多