Search code examples
delphidelphi-7omnixml

How to create simple XML in OmniXML


i want to create this structure xml

<root ver="" file=""> 
  <row> 
    <id></id> 
    <name></name> 
    <surname></surname> 
    <time></time> 
    <old></old> 
    <subject></subject> 
  </row> 
</root> 

when i do the main structure i get the data from online database and i want to add it in my xml file

  1. how can i create the main structure
  2. how can i add a new Row in every update the database made
  3. how can i sort the xml, base of the name node

Please help

Thank you


Solution

  • You can use create inherited classes which define your xml

    interface
    
    uses
      OmniXML, OmniXMLProperties;
    
    type
      TRow = class(TGpXMLData)
      public
        constructor Create(Node: IXMLNode); override;
    
        property Id: integer index 0 read GetXMLPropInt write SetXMLPropInt;
        property Name: WideString index 1 read GetXMLPropWide write SetXMLPropWide;
        property Surname: WideString index 2 read GetXMLPropWide write SetXMLPropWide;
        property Time: WideString index 3 read GetXMLPropWide write SetXMLPropWide;
        property Old: WideString index 4 read GetXMLPropWide write SetXMLPropWide;
        property Subject: WideString index 5 read GetXMLPropWide write SetXMLPropWide;
      end;
    
      TRows = class(TGpXMLList)
      protected
        function GetRow(Value: integer): TRow;
      public
        constructor Create(ParentNode: IXMLNode); reintroduce;
    
        function Add: TRow; reintroduce;
    
        property Rows[Value: integer]: TRow read GetRow; default;
      end;
    
      TRootsXml = class(TGpXmlDocList)
      private
        fRows: TRows;
      public
        constructor Create; reintroduce;
        destructor Destroy; override;
    
        property Ver: WideString index 0 read GetXMLAttrPropWide write SetXMLAttrPropWide;
        property RootFile: WideString index 1 read GetXMLAttrPropWide write SetXMLAttrPropWide;
    
        property Rows: TRows read fRows;
      end;
    
    implementation
    
    constructor TRow.Create(Node: IXMLNode);
    begin
      inherited;
    
      InitChildNodes(['id', 'name', 'surname', 'time', 'old', 'subjects'], 
        ['', '', '', '', '', '']);
    end;
    
    constructor TRows.Create(parentNode: IXMLNode);
    begin
      inherited Create(parentNode, '', 'row', TRow);
    end;
    
    function TRows.Add: TRow;
    begin
      result := TRow(inherited Add);
    end;
    
    function TRows.GetRow(Value: Integer): TRow;
    begin
      Result := TRow(inherited Items[Value]);
    end;
    
    constructor TRootsXml.Create;
    var
      xmlPI: IXMLProcessingInstruction;
    begin
      inherited Create('Root', '', '', nil);
    
      xmlPI := XMLDoc.CreateProcessingInstruction('xml', 'version="1.0" encoding="utf-8"');
      XMLDoc.InsertBefore(xmlPI, node);
    
      InitChildNodes(['ver', 'file'], ['', '']);
    
      fRows := TRows.Create(node);
    end; 
    
    destructor TRootsXml.Destroy;
    begin
      fRows.free;
    
      inherited;
    end;
    

    After that write a procedure to export the data e.g.

    procedure TExport.Exportrows;
    var
      rootsXml: TRootsXml;
      row: TRow;
      i: integer;
    begin
      rootsXml := TRootsXml.Create;
      try
        rootsXml.Ver := 'version 27';
        rootsXml.RootFile := 'fred.exe';
    
        for i := 1 to 10 do
        begin
          row := rootsXml.Rows.Add;
    
          row.Id := i;
          row.Name := 'fred';  
          row.Surname := 'Flintstone';
          row.Time := 'late';
          row.Old := 'very';
          row.Subject := 'Delphi';
        end;
    
        rootsXml.SaveToFile('c:\test\test.xml', ofIndent);
      finally
        rootsXml.free;
      end;
    end;