Author Topic: GetContentsNum() crash when no xml element  (Read 1082 times)

Offline mkvist

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • Email
GetContentsNum() crash when no xml element
« on: June 24, 2009, 08:21:58 »
I have a xml file that looks like this:
Code: [Select]
<ConfigData>
    <productType>
        <Number>1</Number>
        <Name>test</Name>
    </productType>
    <Variable>
        <Name>Reset</Name>
        <Description>Is used for...</Description>
    </Variable>
     <Variable>
        <Name></Name>
        <Description>Is used for...</Description>
    </Variable>
   <Variable>

        <Description>Is used for...</Description>
    </Variable>
    <person>
        <Name>Bill Gates</Name>
        <Description>Is founder of MS</Description>
    </person>

</ConfigData>

Where i have to get the content of all <Variable> elements.
So i have made a loop that runs through the <ConfigData> and triggers when it finds a <Variable> element. Thats great.

To get the value of <Name> and <Description> i first use the following code to check if there is something:

XML xmlEvent("c:\\my.xml");

xmlEvent.GetRootElement()->GetChildren()[1]->FindElementZ("Name")->GetContents()[0];

For the first and the second <Variable> there is no problem, but with the third where there is no <name> element in the <Variable>, then the program crashes.
Is there a way to test if the <name> is present?

Offline Caxorrillo

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: GetContentsNum() crash when no xml element
« Reply #1 on: June 24, 2009, 08:59:47 »
hi mkvist,

I advise you to do some checkings before access the content:

Code: [Select]
//.......

//Having that "pElement" is the "Name" element:
unsigned int uiContentsNum=pElement->GetContentsNum();
char *szContent;
if(uiContentsNum>0)
{

XMLContent *pContent=pElement->GetContents()[0];

//First we get the lenght
unsigned int uiLContent=pContent->GetValue(NULL);

//Then we reserve the memory and get the content value.
szContent=new char[iLContent+1];
pContent->GetValue(szContent);
}
else
{
szContent=NULL;
}

//.........


I Hope it will helps!



Offline mkvist

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • Email
Re: GetContentsNum() crash when no xml element
« Reply #2 on: June 25, 2009, 00:25:21 »
Thanks for the answer, i have put in the missing lines to your code, so it looks like this:

Code: [Select]
//.......

//Having that "pElement" is the "Name" element:
XMLElement *pElement = xmlEvent.GetRootElement()->GetChildren()[1]->FindElementZ("Name");
unsigned int uiContentsNum=pElement->GetContentsNum();
char *szContent;
int iLContent=0;
if(uiContentsNum>0)
{

XMLContent *pContent=pElement->GetContents()[0];

//First we get the lenght
unsigned int uiLContent=pContent->GetValue(NULL);

//Then we reserve the memory and get the content value.
szContent=new char[iLContent+1];
pContent->GetValue(szContent);
cout << "szContent=something" <<  endl;
}
else
{
szContent=NULL;
cout << "szContent=NULL" <<  endl;

}

but my program still crashes if there is no <name> element in the child that i'm searching in.
e.g. if i look for name in this element, the code would crash:

  <Variable>

        <Description>Is used for...</Description>
  </Variable>

Offline Caxorrillo

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: GetContentsNum() crash when no xml element
« Reply #3 on: June 25, 2009, 02:27:50 »
yes mate, you are right.

add a parameter to FindElementZ:

Code: [Select]
XMLElement *pElement = xmlEvent.GetRootElement()->GetChildren()[1]->FindElementZ("Name",[b]TRUE[/b]);

Offline Michael

  • Administrator
  • Super Member
  • ******************
  • Posts: 1069
  • Karma: 32
    • TurboIRC.COM
Re: GetContentsNum() crash when no xml element
« Reply #4 on: July 03, 2009, 05:02:45 »
I am glad you solved it while I was away :)