c++xmltinyxmlnull-pointer

How to handle tinyxml null pointer returned on GetText()


TiXmlElement *pElem;    
std::string StatusResponse;
pElem = hResponse.FirstChild("StatusResponse").Element();

if (pElem)
    StatusResponse = pElem->GetText();

If pElem is valid but the element contains no text, pElem->GetText() returns a NULL pointer, causing an exception. How should I handle this?

Thanks.


Solution

  • if (pElem && pElem->GetText())
        StatusResponse = pElem->GetText();