Generating XML Using DFSORT – Part II

(Originally posted 2006-04-24.)

In Generating XML Using DFSORT – Part I I described how to generate XML using some of DFSORT’s new features.

But the XML we generated was only one of the possible styles of XML: Most of the information was specified as attributes (with the values enclosed in quotes). Here’s a sample line:

<member surname="Mercury" firstname="Freddie" job="Singer" />

In this case the attribute job has the value Singer.

Another style uses elements rather than attributes. Here’s an example that conveys the same information (but in XML terms is not equivalent):

  <member>
    <surname>Mercury</surname>
    <firstname>Freddie</firstname>
    <job>Singer</job>
  </member>

The following example is similar to the one in User Guide for DFSORT PTFs UK90007 and UK90006 (SORTPEUG), but it builds on the example in Part I.

Starting with the same set of data

Mercury         Freddie         Singer   
May             Brian           Guitarist
Taylor          Roger           Drummer  
Deacon          John            Bassist  

and the same Symbols mapping it

Surname,*,16,CH  
Firstname,*,16,CH
Job,*,10,CH

We’d like to create an XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<band>
  <member>
    <surname>Mercury</surname>
    <firstname>Freddie</firstname>
    <job>Singer</job>
  </member>
  <member>
    <surname>May</surname>
    <firstname>Brian</firstname>
    <job>Guitarist</job>
  </member>
  <member>
    <surname>Taylor</surname>
    <firstname>Roger</firstname>
    <job>Drummer</job>
  </member>
  <member>
    <surname>Deacon</surname>
    <firstname>John</firstname>
    <job>Bassist</job>
  </member>
</band>

The first step is to generate the data records (the ones bracketed by <member;> and </member>). Here is the OUTFIL statement that does this:

OUTFIL FNAMES=OUT1,REMOVECC,
  BUILD=(3:C'<member>',/,
         5:Surname,JFY=(LENGTH=35,
           SHIFT=LEFT,LEAD=C'<surname>',TRAIL=C'</surname>'),/,
         5:Firstname,JFY=(LENGTH=39,
           SHIFT=LEFT,LEAD=C'<firstname>',TRAIL=C'</firstname>'),/,
         5:Job,JFY=(LENGTH=27,
           SHIFT=LEFT,LEAD=C'<job>',TRAIL=C'</job>'),/,
         3:C'</member>')

In this case I didn’t actually use the Symbol for Position capability just introduced by UK90006 / UK90007, but I could have. But I did use the JFY function, also new with that level of DFSORT. Its purpose, just like in Part I, is to squeeze out trailing blanks from the fixed-width fields.

NOTE: The use of REMOVECC is important to avoid generating ANSI control characters, which would not be valid XML.

At this stage the output would be

  <member>
    <surname>Mercury</surname>
    <firstname>Freddie</firstname>
    <job>Singer</job>
  </member>
  <member>
    <surname>May</surname>
    <firstname>Brian</firstname>
    <job>Guitarist</job>
  </member>
  <member>
    <surname>Taylor</surname>
    <firstname>Roger</firstname>
    <job>Drummer</job>
  </member>
  <member>
    <surname>Deacon</surname>
    <firstname>John</firstname>
    <job>Bassist</job>
  </member>

To add the first 2 lines and the final line to this we code additional OUTFIL parameters:

  HEADER1=('<?xml version="1.0" encoding="UTF-8" ?>',/,'<band>'),
  TRAILER1=('</band>')

making the entire OUTFIL statement:

OUTFIL FNAMES=OUT1,REMOVECC,
  BUILD=(3:C'<member>',/,
         5:Surname,JFY=(LENGTH=35,
           SHIFT=LEFT,LEAD=C'<surname>',TRAIL=C'</surname>'),/,
         5:Firstname,JFY=(LENGTH=39,
           SHIFT=LEFT,LEAD=C'<firstname>',TRAIL=C'</firstname>'),/,
         5:Job,JFY=(LENGTH=27,
           SHIFT=LEFT,LEAD=C'<job>',TRAIL=C'</job>'),/,
         3:C'</member>'),
  HEADER1=('<?xml version="1.0" encoding="UTF-8" ?>',/,'<band>'),
  TRAILER1=('</band>')

You can use this statement with statements such as SORT, COPY, INCLUDE, OMIT, INREC and OUTREC, so long as the Symbols set you use reflect any reformatting of the records passed into OUTFIL.

In this entry and Part I I’ve shown you a couple of techniques for generating XML, which can be adapted in a mix and match fashion. In the next entry in this series I’ll start to describe how to parse XML.

Published by Martin Packer

.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: