Hi all,
Could someone help me? I have programm that reads xml file and displays it in window with StringGrid. I need to replace my code with a function that has xml file as a parameter and returns two-dementional array of strings (instead of StringGrid). You can see my code below.

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
    {

      XMLDocument1->Active = false;
      if(OpenDialog1->Execute())
       XMLDocument1->LoadFromFile(OpenDialog1->FileName); // xml file load to parser
      XMLDocument1->Active = true;

      int k = 0;
      AnsiString S,S1; // here code receiving attributes of xml nodes
      for (int i = 1; i < XMLDocument1->ChildNodes->Nodes[1]->ChildNodes->Count + 1; i++)
       {
        for (int j = 1; j < XMLDocument1->ChildNodes->Nodes[1]->ChildNodes->Nodes[i-1]->ChildNodes->Count + 1; j++)
        {
         k = k++;

         StringGrid1->RowCount++;
         StringGrid1->Cells[0][k] = XMLDocument1->ChildNodes->Nodes[1]->ChildNodes->Nodes[i-1]->ChildNodes->Nodes[j-1]->NodeName;
         for (int m = 1; m < XMLDocument1->ChildNodes->Nodes[1]->ChildNodes->Nodes[i-1]->ChildNodes->Nodes[j-1]->AttributeNodes->Count + 1; m++)
          {
           if(!XMLDocument1->ChildNodes->Nodes[1]->ChildNodes->Nodes[i-1]->ChildNodes->Nodes[j-1]->AttributeNodes->Nodes[m-1]->NodeValue.IsNull())
           {
            S = XMLDocument1->ChildNodes->Nodes[1]->ChildNodes->Nodes[i-1]->ChildNodes->Nodes[j-1]->AttributeNodes->Nodes[m-1]->NodeValue;
            int i2 = 1;
            int len = S.Length(); S1.Delete(0,len); // here code to catch unwanted simbols
             for (int i1 = 1; i1 <= len; i1++)
              {
               if(S[i1] == '-')
                {
                 i2 = i1; S1 = '-';
                 break;
                }
              }
             for (i2+1; i2 <= len; i2++)
              {
               if(S[i2] == '0') S1 = S1 + S[i2];
               if(S[i2] == '1') S1 = S1 + S[i2];
               if(S[i2] == '2') S1 = S1 + S[i2];
               if(S[i2] == '3') S1 = S1 + S[i2];
               if(S[i2] == '4') S1 = S1 + S[i2];
               if(S[i2] == '5') S1 = S1 + S[i2];
               if(S[i2] == '6') S1 = S1 + S[i2];
               if(S[i2] == '7') S1 = S1 + S[i2];
               if(S[i2] == '8') S1 = S1 + S[i2];
               if(S[i2] == '9') S1 = S1 + S[i2];
              }
            StringGrid1->Cells[m][k] = S1; // filling stringgrid with parameters of xml nodes
            S1.Delete(1,i2);
           }
           else
            StringGrid1->Cells[m][k] = "n/a";
          }
        }
       }
    
      XMLDocument1->Active = false;
    }
    catch(...)
    {
      ShowMessage("error load XML");
    }
}

Why can't you do that yourself?

The loop starting on line 38 is poorly written. Note the use of < symbol instead of <=. Arrays are indexed from 0 up to but not including the number of elements in the array (variable len in this case)

for(i2 = 0; i2 < len; i2++)
{
   if( isdigit(s[i2]) )
   {
         // This is just summing the ascii values of the digits
         // which may be pretty meaningless.  For example: 
         // the digit '1' will be added as the decimal value 49,
         // not 1.
           S1 = S1 + S[i2];
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.