TinyXml源码解析之解决XML中&符号过滤的问题

手头有个C++项目用到了XML解析,我采用的是开源的tinyxml,项目运行过程中发现其对XML节点内容的解析过滤掉了“&”符号,于是乎开始读源码解决问题:


在文件 tinyxmlparser.cpp中 Line 438, 函数const char* TiXmlBase::GetEntity( const char* p, char* value, int* length, TiXmlEncoding encoding )实现代码中有:

<span style="font-size:18px;">// So it wasn't an entity, its unrecognized, or something like that.
        *value = *p;    // Don't put back the last one, since we return it!
        //*length = 1;    // Leave unrecognized entities - this doesn't really work.
                                        // Just writes strange XML.</span>

源码中将*length=1注释掉了,直接导致特殊字符“&”被过滤,若你也遇到同样的问题,

只需要将*length=1放开注释即可,如下:

<span style="font-size:18px;">// So it wasn't an entity, its unrecognized, or something like that.
        *value = *p;    // Don't put back the last one, since we return it!
        *length = 1;    // Leave unrecognized entities - this doesn't really work.  no no no  by yangw
                                        // Just writes strange XML.</span>

重新编译即可解决XML节点内容含有“&”符号的过滤问题,一般数据库密码可能会有此类字符出现。


你可能感兴趣的:(C++,tinyxml)