Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyXML parsing a string in XML format returns NULL?

Tags:

tinyxml

I'm trying to use TinyXML to parse a string with XML format. But the return pointer is always NULL. I'm not sure which part of code is setting wrong.

TiXmlDocument docTemp;
const string strData = "<?xml version=\"1.0\" ?><Hello>World</Hello>";
const char* pTest = docTemp.Parse(strData.c_str(), 0 , TIXML_ENCODING_UTF8);
if(pTest == NULL){
    cout << "pTest is NULL" << endl;
}

It always shows 'pTest is NULL' Any idea?

Thanks a bunch!

like image 407
roboren Avatar asked Dec 09 '25 20:12

roboren


1 Answers

It should return 0 in the case of an error but looks like there is bug in TiXmlBase::SkipWhiteSpace, if there is no character after the closing bracket it returns 0, but if there is a white space or \r or \n it returns the pointer. So you have 2 options add some white character after the closing bracket or modify the following lines in at the beginning of SkipWhiteSpace:

if ( !p || !*p )
{
    return 0;
}

to something like:

if ( !p )
{
   return 0;
}
if (!*p)
{
   return p;
}
like image 151
Jose Avatar answered Dec 15 '25 02:12

Jose