Changeset 171 for trunk


Ignore:
Timestamp:
Jan 30, 2024, 11:24:04 PM (11 months ago)
Author:
chronos
Message:
  • Added: Import from XML file.
  • Added: Lazarus project group.
Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Table.pas

    r170 r171  
    44
    55uses
    6   Classes, SysUtils, Generics.Collections, ComCtrls;
     6  Classes, SysUtils, Generics.Collections, ComCtrls, XMLRead, XMLWrite, DOM;
    77
    88type
     
    4646    procedure SetInputMediaWiki(Text: string);
    4747    procedure SetInputJson(Text: string);
     48    procedure SetInputXml(Text: string);
    4849    procedure SetInput(OutputFormat: TTableFormat; Text: string);
    4950    function GetInputFormats: TTableFormats;
     
    457458end;
    458459
     460procedure TTable.SetInputXml(Text: string);
     461var
     462  Doc: TXMLDocument;
     463  TextStream: TStringStream;
     464  TableNode: TDOMNode;
     465  RowsNode: TDOMNode;
     466  RowNode: TDOMNode;
     467  CellNode: TDOMNode;
     468  NewRow: TRow;
     469  CellName: string;
     470  ColumnIndex: Integer;
     471begin
     472  Clear;
     473  TextStream := TStringStream.Create(Text);
     474  ReadXMLFile(Doc, TextStream);
     475  TableNode := Doc.DocumentElement;
     476  if Assigned(TableNode) and (TableNode.NodeName = 'table') then
     477  with TableNode do begin
     478    RowsNode := FindNode('rows');
     479    if Assigned(RowsNode) then begin
     480      RowNode := RowsNode.FirstChild;
     481      while Assigned(RowNode) and (RowNode.NodeName = 'row') do begin
     482        NewRow := TRow.Create;
     483        CellNode := RowNode.FirstChild;
     484        while Assigned(CellNode) and (CellNode.NodeName = 'cell') do begin
     485          CellName := string(TDOMElement(CellNode).GetAttribute('name'));
     486          ColumnIndex := Columns.IndexOf(CellName);
     487          if ColumnIndex < 0 then begin
     488            Columns.Add(CellName);
     489            ColumnIndex := Columns.Count - 1;
     490          end;
     491
     492          while NewRow.Cells.Count <= ColumnIndex do
     493            NewRow.Cells.Add('');
     494          NewRow.Cells[ColumnIndex] := string(CellNode.TextContent);
     495          CellNode := CellNode.NextSibling;
     496        end;
     497        Rows.Add(NewRow);
     498        RowNode := RowNode.NextSibling;
     499      end;
     500    end;
     501  end;
     502  FreeAndNil(TextStream);
     503  FreeAndNil(Doc);
     504end;
     505
    459506procedure TTable.SetInput(OutputFormat: TTableFormat; Text: string);
    460507begin
     
    463510    tfMediaWiki: SetInputMediaWiki(Text);
    464511    tfJson: SetInputJson(Text);
     512    tfXml: SetInputXml(Text);
    465513    else raise Exception.Create(SUnsupportedFormat);
    466514  end;
     
    469517function TTable.GetInputFormats: TTableFormats;
    470518begin
    471   Result := [tfCsv, tfJson, tfMediaWiki];
     519  Result := [tfCsv, tfJson, tfMediaWiki, tfXml];
    472520end;
    473521
Note: See TracChangeset for help on using the changeset viewer.