Home > Blockchain >  Generating XML file with parent fields between child tables using DEFINE DATASET
Generating XML file with parent fields between child tables using DEFINE DATASET

Time:04-20

I am trying to use DEFINE DATASET to generate an XML file with the following format:

<Parent>
   <Child1>
     <Child1 field>
   </Child1>
   <Parent field>
   <Child2>
      <Child2 field>
   </Child2>
</Parent>

I tried using the PARENT-FIELDS-AFTER statement with PARENT > Child1 but the Parent field comes out after the Child2 table. What is the correct syntax?

CodePudding user response:

What your looking for is the PARENT-FIELDS-AFTER and PARENT-FIELDS-BEFORE attributes of the PARENT-ID-RELATION in the dataset.

If you have more complex temp-tables you might have to address more fields in your definition. But this is a basic working example.

DEFINE TEMP-TABLE ttParent NO-UNDO SERIALIZE-NAME "Parent"
    FIELD parentField AS CHARACTER.

DEFINE TEMP-TABLE ttChild1 NO-UNDO SERIALIZE-NAME "Child1"
    FIELD id AS RECID SERIALIZE-HIDDEN
    FIELD childField AS CHARACTER .
    
DEFINE TEMP-TABLE ttChild2 NO-UNDO SERIALIZE-NAME "Child2"
    FIELD id AS RECID SERIALIZE-HIDDEN
    FIELD childField AS CHARACTER .    
    
DEFINE DATASET dsDataset SERIALIZE-HIDDEN FOR ttParent, ttChild1, ttChild2  
    PARENT-ID-RELATION pir1 FOR ttPArent, ttChild1 PARENT-ID-FIELD ttChild1.id
        PARENT-FIELDS-AFTER(ttParent.parentField)
    PARENT-ID-RELATION pir2 FOR ttPArent, ttChild2 PARENT-ID-FIELD ttChild2.id.
    
CREATE ttParent.
ASSIGN ttParent.parentField = "AAA".

CREATE ttChild1.
ASSIGN ttChild1.id = RECID(ttParent)
       ttChild1.childField = "BBB".  
       
CREATE ttChild2.
ASSIGN ttChild2.id = RECID(ttParent)
       ttChild2.childField = "CCC". 
       
DATASET dsDataset:WRITE-XML("file", "c:/temp/file.xml", TRUE, ?,?,?,?,?,TRUE)       

File.xml:

<Parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Child1>
<childField>BBB</childField>
</Child1>
<parentField>AAA</parentField>
<Child2>
<childField>CCC</childField>
</Child2>
</Parent>
  • Related