在C++中将特殊ASCII字符转换为XML兼容字符串

Convert special ASCII Character to XML Compatible string in C++

本文关键字:转换 XML 字符串 字符 ASCII C++      更新时间:2023-10-16

是否有API C++可以用于将特殊字符转换为XML兼容字符串?示例更改

We're sorry, <your> item is out of stock will not be Δ available  (until next month). ÿ

We're sorry, &#x03C;your&#x03E; item is out of stock will not be &#x03F; available  &#x028;until next month&#x029;. &#x0FF;

让我再解释一下我的问题。我在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] 
= 
{
    "&#x000;",  "&#x001;",  "&#x002;",  "&#x003;",  "&#x004;",  "&#x005;",  "&#x006;",  "&#x007;",  "&#x008;",  "&#x009;",  "&#x00A;",  "&#x00B;",  "&#x00C;",  "&#x00D;",  "&#x00E;",  "&#x00F;",
    "&#x010;",  "&#x011;",  "&#x012;",  "&#x013;",  "&#x014;",  "&#x015;",  "&#x016;",  "&#x017;",  "&#x018;",  "&#x019;",  "&#x01A;",  "&#x01B;",  "&#x01C;",  "&#x01D;",  "&#x01E;",  "&#x01F;",
    "&#x020;",  "&#x021;",  "&#x022;",  "&#x023;",  "&#x024;",  "&#x025;",  "&#x026;",  "&#x027;",  "&#x028;",  "&#x029;",  "&#x02A;",  "&#x02B;",  "&#x02C;",  "&#x02D;",  "&#x02E;",  "&#x02F;",
    "&#x030;",  "&#x031;",  "&#x032;",  "&#x033;",  "&#x034;",  "&#x035;",  "&#x036;",  "&#x037;",  "&#x038;",  "&#x039;",  "&#x03A;",  "&#x03B;",  "&#x03C;",  "&#x03D;",  "&#x03E;",  "&#x03F;",
    "&#x040;",  "&#x041;",  "&#x042;",  "&#x043;",  "&#x044;",  "&#x045;",  "&#x046;",  "&#x047;",  "&#x048;",  "&#x049;",  "&#x04A;",  "&#x04B;",  "&#x04C;",  "&#x04D;",  "&#x04E;",  "&#x04F;",  
    "&#x050;",  "&#x051;",  "&#x052;",  "&#x053;",  "&#x054;",  "&#x055;",  "&#x056;",  "&#x057;",  "&#x058;",  "&#x059;",  "&#x05A;",  "&#x05B;",  "&#x05C;",  "&#x05D;",  "&#x05E;",  "&#x05F;",  
    "&#x060;",  "&#x061;",  "&#x062;",  "&#x063;",  "&#x064;",  "&#x065;",  "&#x066;",  "&#x067;",  "&#x068;",  "&#x069;",  "&#x06A;",  "&#x06B;",  "&#x06C;",  "&#x06D;",  "&#x06E;",  "&#x06F;",
    "&#x070;",  "&#x071;",  "&#x072;",  "&#x073;",  "&#x074;",  "&#x075;",  "&#x076;",  "&#x077;",  "&#x078;",  "&#x079;",  "&#x07A;",  "&#x07B;",  "&#x07C;",  "&#x07D;",  "&#x07E;",  "&#x07F;",  
    "&#x080;",  "&#x081;",  "&#x082;",  "&#x083;",  "&#x084;",  "&#x085;",  "&#x086;",  "&#x087;",  "&#x088;",  "&#x089;",  "&#x08A;",  "&#x08B;",  "&#x08C;",  "&#x08D;",  "&#x08E;",  "&#x08F;",  
    "&#x090;",  "&#x091;",  "&#x092;",  "&#x093;",  "&#x094;",  "&#x095;",  "&#x096;",  "&#x097;",  "&#x098;",  "&#x099;",  "&#x09A;",  "&#x09B;",  "&#x09C;",  "&#x09D;",  "&#x09E;",  "&#x09F;",  
    "&#x0A0;",  "&#x0A1;",  "&#x0A2;",  "&#x0A3;",  "&#x0A4;",  "&#x0A5;",  "&#x0A6;",  "&#x0A7;",  "&#x0A8;",  "&#x0A9;",  "&#x0AA;",  "&#x0AB;",  "&#x0AC;",  "&#x0AD;",  "&#x0AE;",  "&#x0AF;",
    "&#x0B0;",  "&#x0B1;",  "&#x0B2;",  "&#x0B3;",  "&#x0B4;",  "&#x0B5;",  "&#x0B6;",  "&#x0B7;",  "&#x0B8;",  "&#x0B9;",  "&#x0BA;",  "&#x0BB;",  "&#x0BC;",  "&#x0BD;",  "&#x0BE;",  "&#x0BF;",
    "&#x0C0;",  "&#x0C1;",  "&#x0C2;",  "&#x0C3;",  "&#x0C4;",  "&#x0C5;",  "&#x0C6;",  "&#x0C7;",  "&#x0C8;",  "&#x0C9;",  "&#x0CA;",  "&#x0CB;",  "&#x0CC;",  "&#x0CD;",  "&#x0CE;",  "&#x0CF;",  
    "&#x0D0;",  "&#x0D1;",  "&#x0D2;",  "&#x0D3;",  "&#x0D4;",  "&#x0D5;",  "&#x0D6;",  "&#x0D7;",  "&#x0D8;",  "&#x0D9;",  "&#x0DA;",  "&#x0DB;",  "&#x0DC;",  "&#x0DD;",  "&#x0DE;",  "&#x0DF;",  
    "&#x0E0;",  "&#x0E1;",  "&#x0E2;",  "&#x0E3;",  "&#x0E4;",  "&#x0E5;",  "&#x0E6;",  "&#x0E7;",  "&#x0E8;",  "&#x0E9;",  "&#x0EA;",  "&#x0EB;",  "&#x0EC;",  "&#x0ED;",  "&#x0EE;",  "&#x0EF;",
    "&#x0F0;",  "&#x0F1;",  "&#x0F2;",  "&#x0F3;",  "&#x0F4;",  "&#x0F5;",  "&#x0F6;",  "&#x0F7;",  "&#x0F8;",  "&#x0F9;",  "&#x0FA;",  "&#x0FB;",  "&#x0FC;",  "&#x0FD;",  "&#x0FE;",  "&#x0FF;",
};

//函数将所有非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 &#x03C;
            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;
}