IDENTIFICATION DIVISION.
PROGRAM-ID. escape-xml.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ws-e-amp PIC X(5) VALUE '&'.
01 ws-e-lt PIC X(4) VALUE '<'.
01 ws-e-quot PIC X(6) VALUE '"'.
01 ws-in PIC X(100) VALUE 'This <is> a &"string"!'.
01 ws-out PIC X(600).
01 ws-n PIC 9(3) BINARY.
01 ws-in-i PIC 9(3) BINARY.
01 ws-out-i PIC 9(3) BINARY.
PROCEDURE DIVISION.
PERFORM VARYING ws-in-i FROM 1 BY 1
UNTIL ws-in-i > LENGTH OF ws-in
EVALUATE ws-in(ws-in-i:1)
WHEN '&' MOVE LENGTH OF ws-e-amp TO ws-n
MOVE ws-e-amp TO ws-out(ws-out-i:ws-n)
WHEN '<' MOVE LENGTH OF ws-e-lt TO ws-n
MOVE ws-e-lt TO ws-out(ws-out-i:ws-n)
WHEN '"' MOVE LENGTH OF ws-e-quot TO ws-n
MOVE ws-e-quot TO ws-out(ws-out-i:ws-n)
WHEN OTHER MOVE 1 TO ws-n
MOVE ws-in(ws-in-i:1) TO ws-out(ws-out-i:1)
END-EVALUATE
ADD ws-n TO ws-out-i
END-PERFORM
DISPLAY ws-in
DISPLAY ws-out
STOP RUN
.