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