Jump to content


Photo

Xml Export


  • Please log in to reply
No replies to this topic

#1 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 18 March 2005 - 10:46 PM

Hi,

Here some simple XML export (could be improved but my time is limted).
It simply exports all columns and cells as plain text. Note that escaping illegal character isn't implemented here. If i have some more time i'll post a DOM based one too.

CODE
procedure TForm1.ExportToXml(const GridView: TGridView; const fn: string);

var

 r, c              : Integer;

 sl                : TStringList;

 s                 : string;



 function EscapeValue(const value: string): string;

 begin

 //Not implemented yet.

   Result := value;

 end;



begin

 sl := TStringList.Create;



 sl.Add('<?xml version="1.0"?>');

 sl.Add(Format('<GridView version = "%s ">', ['2.4.1'])); //Should be stVersion!



 sl.Add(' <Columns>');

 for c := 0 to Pred(GridView.Columns.Count) do

   begin

     sl.Add('  <Column>');

     sl.Add('    ' + EscapeValue(GridView.Columns[c].Header.Caption));

     sl.Add('  </Column>');

   end;

 sl.Add(' </Columns>');



 sl.Add(' <Rows>');

 for r := 0 to Pred(GridView.RowCount) do

   begin

     sl.Add('  <Row>');

     for c := 0 to Pred(GridView.Columns.Count) do

       begin

         sl.Add('   <Cell>');

         sl.Add('    ' + EscapeValue(GridView.Cell[c, r].AsString));

         sl.Add('   </Cell>');

       end;

     sl.Add('   </Row>');

   end;

 sl.Add(' </Rows>');



 sl.Add('</GridView>');



 sl.SaveToFile(fn);

 sl.Free;

end;


Sample output:

CODE
 <?xml version="1.0" ?>

  <GridView version="2.4.1">

   <Columns>

    <Column>Col0</Column>

    <Column>Col1</Column>

  </Columns>

  <Rows>

   <Row>

    <Cell>0</Cell>

    <Cell>0</Cell>

   </Row>

   <Row>

    <Cell>2</Cell>

    <Cell>6</Cell>

   </Row>

  </Rows>

 </GridView>


Have fun coding!
G.W. van der Vegt




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users