Reputation: 314
Is there an API C++ which I can use to convert special character to XML comptible string? Example change
We're sorry, <your> item is out of stock will not be Δ available (until next month). ÿ
to
We're sorry, <your> item is out of stock will not be ? available (until next month). ÿ
Let me explain a bit more about my problem. I work on a Legacy server, which pulls out data reports in Flat files. In to older versions of our client Application, everything works fine with these special chars like <, >, ( etc.
We now are developing a new client, but this client accepts a XML string and render the report on a PHP page. So we made changes to allow the system to output a XML file. But when the XML reaches the client, and if the string contains a funny char like < or ) etc, the DOM parser inside the PHP page starts throwing error. What I want to do is while the XML fine is being created, and if some such special chars are I need to display them in the report hence need to escape < as <
I API a function InternetCanonicalizeUrl which will change a string for URL encoding. I want something similar for XML
Upvotes: 0
Views: 2480
Reputation: 314
Create an arrage of XML encoded 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", "{", "|", "}", "~", "",
"€", "", "‚", "ƒ", "„", "…", "†", "‡", "ˆ", "‰", "Š", "‹", "Œ", "", "Ž", "",
"", "‘", "’", "“", "”", "•", "–", "—", "˜", "™", "š", "›", "œ", "", "ž", "Ÿ",
" ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯",
"°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿",
"À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï",
"Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß",
"à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
"ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ",
};
//Function converts, all non XML allowable ASCII chars to //XML encoded string
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]]);
}
}
}
Upvotes: 1
Reputation: 1
Could you please clarify your question?
I'm not sure why you want to use any sort of API. API is an interface you build to extract data from a system. In any case, For processing a string like that, you could make use of switch case.
Could go like:
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;
}
Upvotes: 0