Problem with & in RPGLE

5 pts.
Tags:
RPGLE
XML
XML-SAX
I'm using XML-SAX in a RPGLE-program to read an XML-file which works pretty fine. Though, when I come to an entity with "Boys "&" Girls" (I added extra " in this example between the "&" just to make it visual)  i run into trouble. The handler does not seem to recognise anything after "Boys" and the stringlength is set to 4.

I have tried to work with *XML_ATTR_PREDEF_REF, but that does not help me since the eventcode actually is *XML_CHARS. Any kind of help would be most appreciated! 

Kind regards RobertW



Software/Hardware used:
RPGLE

Answer Wiki

Thanks. We'll let you know when a new response is added.

The ampersand is a special character in XML. A while back, we wrote the following sub procedure for an XML project we were working on. It receives the string and the position of the ampersand. It then breaks the string into two pieces at the ampersand, adds the special XML symbol for the ampersand and then concatenates the two strings back together. You should be able to modify it do suit your needs.

<pre>
‚**********************************************************************
‚* SET COMPILER OPTIONS
‚**********************************************************************
HNOMAIN
HOPTION(*NODEBUGIO:*SRCSTMT)
‚**********************************************************************
‚* PROGRAM NAME: AMPERSANDR
‚* CREATION DATE: 07/10/07
‚* PURPOSE OF PROGRAM: THE “&” IS A SPECIAL CHARACTER IN XML. THIS
‚* PROCEDURE WILL WRAP THE & IN A FORMAT THAT IS
‚* ACCEPTABLE TO XML.
‚*********************************************************************
‚* PROTOTYPE FOR PROCEDURE
‚*********************************************************************
D AMPERSANDR PR 50
D DESCRIPTION 50
D AMPER_POS 5 0
‚*********************************************************************
‚* START PROCEDURE DEFINITION
‚*********************************************************************
P AMPERSANDR B EXPORT

D AMPERSANDR PI 50
D DESCRIPTION 50
D AMPER_POS 5 0
‚*********************************************************************
‚* FIELD DEFINITIONS
‚*********************************************************************
D HOLD_DESC1 S 35 INZ(*BLANKS)
D HOLD_DESC2 S 35 INZ(*BLANKS)
D NEWDESCRIPTION…
D S 50 INZ(*BLANKS)
D AMPERSAND_SYMBOL…
D S 5 INZ(‘&’)
‚*********************************************************************
‚* REPLACE & WITH XML EQUILIVENT.
‚*********************************************************************

/FREE

HOLD_DESC1 = %TRIM(%SUBST(DESCRIPTION : 1 : AMPER_POS – 1));
HOLD_DESC2 = %TRIM(%SUBST(DESCRIPTION : AMPER_POS + 1));

NEWDESCRIPTION = %TRIM(HOLD_DESC1) + ‘ ‘ + AMPERSAND_SYMBOL +
‘ ‘ + %TRIM(HOLD_DESC2);

RETURN NEWDESCRIPTION;

/END-FREE

P AMPERSANDR E

</pre>

Discuss This Question: 6  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • BigKat
    or try this procedure. It is i6.1 Remove the "len(4000000)" and set the size for your needs
         h option(*nodebugio:*srcstmt) nomain
    
         d htmlSafe        pr              a   len(4000000) varying
         d  textIn                         a   len(4000000) varying const
    
         p htmlSafe        b                   export
         d htmlSafe        pi              a   len(4000000) varying
         d  textIn                         a   len(4000000) varying const
    
         d wrkText         s               a   len(4000000) varying
         d position        s             20i 0
         d done            s               n
    
          /free
               wrkText = textIn;
               dou done;
                    position = %scan('&':wrkText:position + 1);
                    if position = 0;
                         leave;
                    endif;
                    wrkText = %replace('&':wrkText:position:1);
               enddo;
               dou done;
                    position = %scan('"':wrkText:position + 1);
                    if position = 0;
                         leave;
                    endif;
                    wrkText = %replace('"':wrkText:position:1);
               enddo;
               dou done;
                    position = %scan('<':wrkText:position + 1);
                    if position = 0;
                         leave;
                    endif;
                    wrkText = %replace('<':wrkText:position:1);
               enddo;
               dou done;
                    position = %scan('>':wrkText:position + 1);
                    if position = 0;
                         leave;
                    endif;
                    wrkText = %replace('>':wrkText:position:1);
               enddo;
               return wrkText;
    
          /end-free
         p htmlSafe        e
    
    7,845 pointsBadges:
    report
  • BigKat
    Ach! The code block translated my literals in the %replace(' '...
    '&' should be '& a m p ;'  no spaces
    '"' should be '& q u o t ;'  no spaces
    '<' should be '& l t ;'  no spaces
    '>' should be '& g t ;'  no spaces
    
    7,845 pointsBadges:
    report
  • Warleb
    Nice solution. Just what I needed also. But I find that when I have a string such as 'John & CO & Johnson' it only takes the first '&' which is then output like this. 'John & CO & Johnson' I'm not a free form expert by any means but can't get it to handle if more than one '&' in on the same string
    20 pointsBadges:
    report
  • Warleb
    Oops, I solved it
    20 pointsBadges:
    report
  • tantart
    Hi BigKat, NICE solution. Thanks alot
    40 pointsBadges:
    report
  • BigKat
    you're welcome Tantart! Glad it could help! :)
    7,845 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following