将字符编码为 XML 的字符实体
我有一个变量定义为:
D content 1280A CONST
我需要找到 "
, &
, '
, <
, >
字符并将其替换为:
"
、&
、'
、 <
和 >
;
我已经看到了该语言中的一些 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('&' : '&' : 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这对我来说很简单,但是仅仅使用内置的 %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.)
有一个 %scanrpl 函数可以用另一个字符串替换所有出现的字符串。看起来很热门的门票。
There is a %scanrpl function that will replace all occurrences of a string with another string. It looks like the hot ticket.