Reputation: 919
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!
Upvotes: 3
Views: 4069
Reputation: 41
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;
}
Upvotes: 4
Reputation: 71
if(pTest == NULL && docTemp->Error() ){
cout << "pTest is NULL" << endl;
}
Upvotes: 3
Reputation: 11
Looks like TiXMLDocument::Parse
returns NULL
in the case of failure and the pointer to the character next to closing angle bracket, when parsing was succesful.
Upvotes: 1
Reputation: 24413
It seems like the parse returns null on success.
Can you see if docTemp.RootElement() contains a valid element ?
Upvotes: 1