tinyxml2 icon indicating copy to clipboard operation
tinyxml2 copied to clipboard

tinyxml2 6.2.0 RootElement(): returns NULL / nullptr

Open avmnn opened this issue 7 years ago • 0 comments

I am trying to read the following XML file:

<?xml version="1.0" encoding="UTF-8"?>

<Header>
    <SNMP/>
    <IP_address_1/>
    <IP_address_2/>
    <Port_1>161</Port_1>
    <Port_2>162</Port_2>
</Header>

The code with tinyxml2 usage is as follows:

int main (int argc, char* argv[])
{
   tinyxml2::XMLError eResult;
   tinyxml2::XMLDocument xml_file;
   tinyxml2::XMLElement* xml_root;
   tinyxml2::XMLElement* xml_element;
   
   eResult = xml_file.LoadFile("MyConfig.xml");
   
   if (eResult != tinyxml2::XML_SUCCESS)
   {
       return 1;
   }
   
   if ((xml_root = xml_file.RootElement()) != nullptr)
   {
       if ((xml_element = xml_root->FirstChildElement("SNMP")) != nullptr)
       {
       }
       if ((xml_element = xml_root->FirstChildElement("IP_address_1")) != nullptr)
       {
       }
       if ((xml_element = xml_root->FirstChildElement("IP_address_2")) != nullptr)
       {
       }
       if ((xml_element = xml_root->FirstChildElement("Port_1")) != nullptr)
       {
           cout << xml_element->GetText() << endl;
       }
       if ((xml_element = xml_root->FirstChildElement("Port_2")) != nullptr)
       {
           cout << xml_element->GetText() << endl;
       }   
   }
   return 0;
}

And when I try to call 'xml_element->GetText()' I get the following exception: access violation reading by address 0x00000000. The debugging routines permanently show that _rootAttribute = 0x00000000.

Could you please advice?

avmnn avatar Jun 07 '18 11:06 avmnn