分享

perl中XML中非法字符的处理

 hh3755 2012-11-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/&/&amp;/g;
    
$origin =~s/</&lt;/g;
    
$origin =~s/>/&gt;/g;
    
$origin =~s/'/'/g;
    $origin =~s/"/"/g;
    
    return($origin);
}

$string=qq{< and > and & and ' and "};
print ConvertEntity($string);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多