perl中XML中非法字符的处理
10-23
即XML的Tag中间的内容如果包含如下字符的话( & ' " ),需要分别替换为对应的(& < > ' "),否则XML被解析时会报错。
1. 非法字符及替换
& => The ampersand (&).
< => The less than sign (
> => The greater than sign (>).
' => The single quote or apostrophe (').
" => The double quote (").
2. Perl替换程序
sub ConvertEntity
{
my ($origin)=@_;
# < => The less than sign (
# > => The greater than sign (>).
# & => The ampersand (&).
# ' => The single quote or apostrophe (').
# " => The double quote (").
$origin =~s/&/&/g;
$origin =~s/
$origin =~s/>/>/g;
$origin =~s/'/'/g;
$origin =~s/"/"/g;
return($origin);
}
$string=qq{ and & and ' and "};
print ConvertEntity($string);