Wednesday, March 7, 2012

RPGLE Free XML Special Characters code


This is how I overcame the XML special characters in my descriptions. I sure wish I could have figured out how to have this code in a module and bind to my main program. I will get there this weekend.


     d wrkText         s             50a
     d position          s             20i 0
     d done              s                n  

       
/free
        clear wrktext;               //-------
        wrktext = ides;
        exsr xmlspcSR;               // Find XML special characters and replace  



// Fix XML special chararters ************************

        begsr xmlspcSR;

           dou done;
                position = %scan('&':wrkText:position + 1);
                if position = 0;
                     leave;
                endif;
                wrkText = %replace(' & ':wrkText:position:1);
                position = position + 7;
           enddo;

           dou done;
                position = %scan('"':wrkText:position + 1);
                if position = 0;
                     leave;
                endif;
                wrkText = %replace(' " ':wrkText:position:1);
                position = position + 8;
           enddo;

           dou done;
                position = %scan('<':wrkText:position + 1);
                if position = 0;
                     leave;
                endif;
                wrkText = %replace(' &lt; ':wrkText:position:1);
                position = position + 6;
           enddo;

           dou done;
                position = %scan('>':wrkText:position + 1);
                if position = 0;
                     leave;
                endif;
                wrkText = %replace(' &gt; ':wrkText:position:1);
                position = position + 6;

           enddo;
        ENDSR;                                              

/end-free

~Richard

4 comments:

  1. just stumbled across this blog entry randomly... I did a similar thing cleaning up data from file names in the IFS. I wrote this code which is much shorter and uses the %XLATE bif to replace on a character by character basis... I wrote it as a little program that recieves a 256A string parm, cleans it and returns it... it might help you as a shorter code alternative....

    D Dirty c Const(' ¢!~@#$%&*<>()+"|:;')
    D Clean c Const('___________________')
    D Quote C Const(X'7D')
    D UnderLine C Const(' ')


    // define *entry plist parms

    D EntryPList PR Extpgm('SX4CLNIFS')
    D ParmString 256A
    D EntryPlist PI
    D ParmString 256A

    then in your mainline just:

    // Clean out any duff characters
    // and switch to UNDERSCORES
    ParmString = %Xlate( Dirty : Clean : ParmString);
    ParmString = %Xlate( Quote : UnderLine : ParmString);

    Hope it helps ;)

    ReplyDelete
    Replies
    1. Thanks, I will mess around with it this weekend.

      Delete
    2. Damery, never got back to it. Being jack of all trades master of none has it's drawbacks. If it works I have to go with it and move on. Nick posted a great alternate, obviously the real programmer here. Exactly what result are you looking for?

      Delete