'
"
&
<
>
當遇到以上幾個字元時,必須做以下轉換:
'
"
&
<
>
以C++寫法為例,輸入一個string型態的字串,回傳已經做好特殊字元取代的字串
std::string XmlEscape(const std::string &rstrString)
{
ostringstream ossEscapeString;
unsigned char ch;
for(string::const_iterator iter = rstrString.begin(); iter != rstrString.end(); iter++ )
{
ch = static_cast
switch(ch)
{
case '&':
ossEscapeString << "&";
break;
case '<':
ossEscapeString << "<";
break;
case '>':
ossEscapeString << ">";
break;
case '"':
ossEscapeString << """;
break;
case '\'':
ossEscapeString << "'";
break;
default:
ossEscapeString << ch;
break;
}
}
return ossEscapeString.str();
}
沒有留言:
張貼留言