在C++中将特殊ASCII字符转换为XML兼容字符串
Convert special ASCII Character to XML Compatible string in C++
是否有API C++可以用于将特殊字符转换为XML兼容字符串?示例更改
We're sorry, <your> item is out of stock will not be Δ available (until next month). ÿ
至
We're sorry, <your> item is out of stock will not be ? available (until next month). ÿ
让我再解释一下我的问题。我在Legacy服务器上工作,它以平面文件的形式提取数据报告。在我们的客户端应用程序的旧版本中,使用这些特殊字符(如<,>),一切都很好,(等
我们现在正在开发一个新的客户端,但是这个客户端接受一个XML字符串并在PHP页面上呈现报告。因此,我们进行了更改,允许系统输出XML文件。但是当XML到达客户端时,如果字符串包含一个有趣的字符,比如<或者)等,PHP页面内的DOM解析器开始抛出错误。我想做的是在创建XML fine的同时,如果有一些这样的特殊字符,我需要在报告中显示它们,因此需要转义<作为&x03C
I API函数InternetCanonicalizeUrl将更改URL编码的字符串。我想要类似的XML
创建一个XML编码的ASCII 数组
//ASCII to To XML Encoding char map.
//Each index in the array represents a ASCII char, and the corresponding XML
//endcoded string.
//AB 2013/08/02
static const char m_arrAsciiMap[256][8]
=
{
"�", "", "", "", "", "", "", "", "", "	", "
", "", "", "
", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
" ", "!", """, "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?",
"@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\", "]", "^", "_",
"`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "",
"€", "", "‚", "ƒ", "„", "…", "†", "‡", "ˆ", "‰", "Š", "‹", "Œ", "", "Ž", "",
"", "‘", "’", "“", "”", "•", "–", "—", "˜", "™", "š", "›", "œ", "", "ž", "Ÿ",
" ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯",
"°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿",
"À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï",
"Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß",
"à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
"ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ",
};
//函数将所有非XML允许的ASCII字符转换为//XML编码字符串
void XMLEncodeString(char *pDestBuffer, char *SourceBuffer)
{
int buffLen = strlen(SourceBuffer);
int CurrentPointerPos = 0;
for(int i = 0; i < buffLen; i++)
{
if ((((BYTE)SourceBuffer[i]) >= 32 && ((BYTE)SourceBuffer[i]) <= 37)
|| (((BYTE)SourceBuffer[i]) == 39 )
|| (((BYTE)SourceBuffer[i]) >= 42 && ((BYTE)SourceBuffer[i]) <= 59)
|| (((BYTE)SourceBuffer[i]) >= 64 && ((BYTE)SourceBuffer[i]) <= 122))
{
//Check if the Chars are allowed, if yes then dont convert to XML encoded string
//Numbers, Alphabets upper and lower case can be ignored, certain special chars
// can also be ignored
pDestBuffer[CurrentPointerPos] = SourceBuffer[i];
CurrentPointerPos++;
}
else
{
//If the char is not allowed in XML string convert it to the XML encoded equivalent.
//Replace the single char with the XML encoded string e.g < with <
memcpy((pDestBuffer + CurrentPointerPos), m_arrAsciiMap[(BYTE)SourceBuffer[i]], strlen(m_arrAsciiMap[(BYTE)SourceBuffer[i]]));
CurrentPointerPos += strlen(m_arrAsciiMap[(BYTE)SourceBuffer[i]]);
}
}
}
你能澄清你的问题吗?
我不知道你为什么要使用任何类型的API。API是您为从系统中提取数据而构建的接口。在任何情况下,为了处理这样的字符串,您都可以使用switch case。
可能类似于:
switch ( <variable> ) {
case this-value:
Code to execute if <variable> == this-value
break;
case that-value:
Code to execute if <variable> == that-value
break;
...
default:
Code to execute if <variable> does not equal the value following any of the cases
break;
}
相关文章:
- 将 QGraphicsView 或 Scene 转换为 XML/JSON
- 是否可以将信息从XML文件转换为CPP
- 将字符串转换为 XML 并使用提升更新值字段
- 将 RTF 表转换为 xml
- 如何将XML节点数据转换为QT中的字符串
- 编译C 源的问题将.OFF文件转换为XML Android OpenGL ES可读取的问题
- 如何使用Visual C 6转换XML和XSLT
- XSLT 转换为 C++ 以从 XML 获取 JSON
- 有没有办法修改样式表,以便它将带有空标记的 XML 文档转换为 <tag />?
- XML 到 JSON 的转换 C++
- 如何将已写入 xml 文件的 OpenCV 垫子转换回图像
- qt项目代码到xml文件的转换
- C#.Net 4.0应用程序中托管的C++ActiveX控件中出现Xml.Serializer非法强制转换异常
- Xerces XML解析器,将XMLCh*转换为float
- 在C++中将特殊ASCII字符转换为XML兼容字符串
- 在Linux中使用终端将XML文件转换为c++
- 如何将所有xml格式化后的xmlNodePtr转换为字符串
- 如何使用tinyxml将xml属性转换为c++类
- 在c++中有办法把JSON转换成XML吗?
- 用于c++的XML解析器,它将XML节点转换为c++对象