Skip to main content

Problem:

Attempting to read in an XML document, move i's elements to a new output XML file with the same layout, and write all the records to the new file.

The document file contains one root element called <letteroutput> and this is speciified at the 01 level of the record description.  Within this one root element there can be any number of child elements called <dunning_letter> which itself contains other child elements and attributes.

Reading through the input file using READ NEXT KEY DUNNING-LETTER, and then was moving the elements of each record to the output record, followed by a WRITE LETTER-OUTPUT, and ending up with the <letteroutput> tag for each id (key).

           move c-type         to o-type

           move c-id           to o-id

           move c-alt-id       to o-alt-id

           move c-currency     to o-currency

           move c-timestamp    to o-timestamp

           move c-name         to o-name

           move c-address1     to o-address1

           move c-address2     to o-address2

           move c-city         to o-city

           move c-county       to o-county

           move c-state        to o-state

           move c-postal-code  to o-postal-code

           move c-country      to o-country

           move c-pay-due-date to o-pay-due-date

           move c-balance      to o-balance

           move c-check-id     to o-check-id

           move c-amount       to o-amount

           write o-letteroutput

           if xml-file-status < 0

              display 'Error on write of output xml'

           end-if.

Resolution:

When writing out multiple child element records within one containing root element, you must write each of the child elements using the WRITE KEY ALL child-element syntax, prior to writing the 01 level element to disk using WRITE LETTER-OUTPUT.

The following construct must be used to achieve the desired results:

           open output o-file    *> Create the XML tree in memory

           open input i-file    

           read i-file                 *> Reads entire file into XML tree

           start i-file key C-DUNNING-LETTER  *> establish key of reference

Do the following for each record that you want to add to the file.

           perform until exit

              read i-file next key C-DUNNING-LETTER

              if xml-file-status = -7    *> end of file

                 exit perform

              end-if

              move c-type         to o-type

              move c-id           to o-id

              ...

              move c-currency     to o-currency

              move c-name         to o-name

              move c-address1     to o-address1

              

              write o-letteroutput

                 key is ALL O-DUNNING-LETTER

              if xml-file-status < 0

                 display 'Error on write of output xml'

              end-if

           end-perform

This now has created the external representation in memory but has not written anything to the file.

To then write the file containing all records do:

           write o-letteroutput

           close o-file

Old KB# 7045