unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Controls, Forms,
  DB,DBTables,ComObj,SHDocVw, ExtCtrls,ActiveX, OleCtrls,MSXML_TLB, ADODB,
  StdCtrls, Grids, DBGrids,Dialogs,XMLDoc;
type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    ADOQuery2: TADOQuery;
    DBGrid2: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure DBGrid1DblClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
     procedure showXML1;
     function checkTYPE(field:Tfield;datatype:Tfieldtype):string;   //Check field types
    { Public declarations }
  end;var
  doc : IXMLDOMDocument;
  root,child,child1 : IXMLDomElement;
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DBGrid1DblClick(Sender: TObject);
var
str:string;
begin
       str:=dbgrid1.SelectedField.AsVariant;
       ADOQuery2.SQL.Text:='select * from gzqk where ygid='+str+'';
       adoquery2.Active:=true;
end;
procedure TForm1.showXML1;
var
xml,temp:string;
i,j:integer;
begin
  try
  xml:='yuangong';
  temp:='';
  doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument;
  root:=doc.createElement(xml);
  with ADOquery1 do
  begin
    adoquery1.sql.Clear;
    adoquery1.SQL.Text:='select * from yuangong';
    adoquery1.Open;
    adoquery1.First;
    while not adoquery1.Eof do
    begin
         child:= doc.createElement('Records');
         root.appendChild(child);
         for i:=0 to adoquery1.FieldCount-1 do
         begin
          child1:=doc.createElement(adoquery1.Fields[i].FieldName);
          child.appendchild(child1);
          temp:=checkTYPE(adoquery1.Fields[i],adoquery1.Fields[i].DataType);
          child1.appendChild(doc.createTextNode(temp));
         end;
         //doc.save(xml+'.xml');
        {with adoquery2 do
         begin
           adoquery2.sql.Clear;
           adoquery2.SQL.Text:='select * from gzqk where ygid='+adoquery1.fieldbyname('id').AsString+'';
           adoquery2.Open;
           adoquery2.First;
           while not adoquery2.Eof do
           begin
               child:= doc.createElement('Records');
               root.appendChild(child);
               for j:=0 to adoquery2.FieldCount-1 do
               begin
                child1:=doc.createElement(adoquery2.Fields[j].FieldName);
                child.appendchild(child1);
                temp:=checkTYPE(adoquery2.Fields[j],adoquery2.Fields[j].DataType);
                child1.appendChild(doc.createTextNode(temp));
               end;
            adoquery2.Next;
           end;
         end;}
         adoquery1.Next;
    end;
    doc.save(xml+'.xml');
  end;
  except
      on e:Exception do
      exit;
  end;
end;
function Tform1.checkTYPE(field:Tfield;datatype:Tfieldtype):string;
var
temp:string;
begin
      result:='';
      case TFieldType(Ord(datatype)) of
            ftString,ftWideString:
            begin
              if field.AsString ='' then
                  temp :='null' //Put a default string
              else
                  temp := field.AsString;
              end;
              ftInteger,ftAutoInc,ftWord, ftSmallint:
              begin
              if field.AsInteger > 0 then
                  temp := IntToStr(field.AsInteger)
              else
                  temp := '0';
              end;
              ftFloat, ftCurrency, ftBCD:
              begin
                  if field.AsFloat > 0 then
                      temp := FloatToStr(field.AsFloat)
                  else
                      temp := '0';
              end;
              ftBoolean:
              begin
              if field.Value then
                  temp:= 'True'
              else
                  temp:= 'False';
              end;
              ftDate:
              begin
              if (not field.IsNull) or (Length(Trim(field.AsString)) > 0) then
                  temp := FormatDateTime('MM/DD/YYYY',field.AsDateTime)
              else
                  temp:= '01/01/2000'; //put a valid default date
              end;
              ftDateTime:
              begin
                  if (not field.IsNull) or (Length(Trim(field.AsString)) > 0) then
                      temp := FormatDateTime('MM/DD/YYYY hh:nn:ss',field.AsDateTime)
                  else
                      temp := '01/01/2000 00:00:00'; //Put a valid default date and time
                  end;
              ftTime:
              begin
                  if (not field.IsNull) or (Length(Trim(field.AsString)) > 0) then
                        temp := FormatDateTime('hh:nn:ss',field.AsDateTime)
                  else
                        temp := '00:00:00'; //Put a valid default time
                  end;
              end;
              result:=temp;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
     showXML1;
end;end.