开发者

Encode characters into character entities for XML

开发者 https://www.devze.com 2023-02-05 20:14 出处:网络
I have a variable defined as: D content1280ACONST I need to find the \", &, \', <, > characters and replace them with:

I have a variable defined as:

D content                     1280A   CONST 

I need to find the ", &, ', <, > characters and replace them with:

&quot;, &amp;, &apos;, &lt;, and &gt; respectively.

I've seen some XML functions in the language but these do not seem to be what I need. But I could be wrong so here I am asking.

Using RPGLE, freeform.

Solution: Perhaps not very RPG-ish but it worked

P encode          B             
D                 PI          1280A        
D content                     1280A   CONST    
D outStr          S           1280A                       
D strDsp          S             50A 
 /free 
   outStr = %trim(content);   
   outStr = replaceAll('&' : '&amp;' : outStr);  
   outStr = replaceAll('"' : '&quot;' : outStr);  
   outStr = replaceAll('''' : '&apos;' : outStr);     
   outStr = replaceAll('>' : '&gt;' : outStr);    
   outStr = replaceAll('<' : '&lt;' : outStr);   
   return outStr;                               
 /end-free                     
P                 E        


P*** Procedure: replaceAll  ************************************  
P*** IN: character to replace, replacement text, source       
P*** OUT: transformed s开发者_如何转开发tring             
P replaceAll      B                          
D                 PI          1280A          
D character                      1A   CONST           
D rText                         10A   CONST    
D content                     1280A   CONST         
D outStr          S           1280A   
D dspStr          S             50A                      
D rSize           S              3P 0                                       //replacement text size
D index           S              3P 0                                       //cur str index  
D cntSize         S              3P 0                                       //content size
 /free                                                   
   rSize = %len(%trim(rText));                               
   cntSize = %len(%trim(content));                          
   outStr = content;                                            
   for i = 1 to cntSize; //scan starts at character 1 not 0
      index = %scan(character : outStr : i);
      if index = 0;            
    leave;                        
      endif;                                   
      outStr = %replace(%trim(rText) : outStr : index : 1);    
      i = index + 1;                    
   endfor;          
   return outStr; 
 /end-free 
P                 E         


Maybe this is simple-minded of me, but would it be enough to just use the built-in %replace function? I mean, you'd have to use it repeatedly, for the different things you're replacing. But are there any special cases that kind of defeat mindless replacement? (I'm thinking of how often people try to just parse CSVs by mindlessly breaking on commas, for example. That doesn't go well for some data sets.)


There is a %scanrpl function that will replace all occurrences of a string with another string. It looks like the hot ticket.

0

精彩评论

暂无评论...
验证码 换一张
取 消