将字符编码为 XML 的字符实体

发布于 2024-10-13 11:11:26 字数 2563 浏览 10 评论 0原文

我有一个变量定义为:

D content                     1280A   CONST 

我需要找到 ", &, ', <, > 字符并将其替换为:

&quot;&&apos;&lt;>;

我已经看到了该语言中的一些 XML 函数,但这些似乎不是我所需要的。可能是错误的,所以我在这里问

使用RPGLE,自由形式

:也许不是很像RPG,但它有效。

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 string             
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         

I have a variable defined as:

D content                     1280A   CONST 

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

", &, ', <, and >; 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('&' : '&' : outStr);  
   outStr = replaceAll('"' : '"' : outStr);  
   outStr = replaceAll('''' : ''' : outStr);     
   outStr = replaceAll('>' : '>' : outStr);    
   outStr = replaceAll('<' : '<' : outStr);   
   return outStr;                               
 /end-free                     
P                 E        


P*** Procedure: replaceAll  ************************************  
P*** IN: character to replace, replacement text, source       
P*** OUT: transformed string             
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         

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-10-20 11:11:26

也许这对我来说很简单,但是仅仅使用内置的 %replace 函数就足够了吗?我的意思是,对于要替换的不同东西,您必须重复使用它。但有没有什么特殊情况是那种无意识的失败替换呢? (例如,我在想人们经常试图通过无意识地中断逗号来解析 CSV。这对于某些数据集来说效果并不好。)

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

差↓一点笑了 2024-10-20 11:11:26

有一个 %scanrpl 函数可以用另一个字符串替换所有出现的字符串。看起来很热门的门票。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文