5 pts.
 Problem with & in RPGLE
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
ASKED: September 4, 2009  9:10 AM
UPDATED: January 13, 2011  3:13 PM
  Help
 Approved Answer - Chosen by MelanieYarbrough

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
ANSWERED:  Sep 9, 2009  3:19 PM (GMT)  by MelanieYarbrough

 
Other Answers:

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>

Last Wiki Answer Submitted:  September 8, 2009  2:22 pm  by  Teandy   5,830 pts.
Latest Answer Wiki Contributors:  Teandy   5,830 pts.
To see other answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

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,185 pts.

 

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 pts.

 

Oops, I solved it

 20 pts.

 

Hi BigKat, NICE solution. Thanks alot

 40 pts.

 

you’re welcome Tantart! Glad it could help! :)

 7,185 pts.