function TForm1.makeXml(table: TTable): Integer; var i : Integer; xml,temp : String; begin try table.close; table.open; xml := table.TableName; doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument; //Set the root name of the xml file as that of the table name. //In this case "country" root := doc.createElement(xml); doc.appendchild(root); //This while loop will go through the entire table to generate the xml file while not table.eof do begin //adds the first level children , Records child:= doc.createElement('Records'); root.appendchild(child); for i:=0 to table.FieldCount-1 do begin //adds second level children child1 := doc.createElement (table.Fields[i].FieldName); child.appendchild(child1); //Check field types case TFieldType (Ord(table.Fields[i].DataType)) of ftString: begin if Table.Fields[i].AsString ='' then temp :='null' //Put a default string else temp := table.Fields[i].AsString; end; ftInteger, ftWord, ftSmallint: begin if Table.Fields[i].AsInteger > 0 then temp := IntToStr(table.Fields[i].AsInteger) else temp := '0'; end; ftFloat, ftCurrency, ftBCD: begin if table.Fields[i].AsFloat > 0 then temp := FloatToStr(table.Fields[i].AsFloat) else temp := '0'; end; ftBoolean: begin if table.Fields[i].Value then temp:= 'True' else temp:= 'False'; end; ftDate: begin if (not table.Fields[i].IsNull) or (Length(Trim(table.Fields[i].AsString)) > 0) then temp := FormatDateTime('MM/DD/YYYY', table.Fields[i].AsDateTime) else //put a valid default date temp:= '01/01/2000'; end; ftDateTime: begin if (not table.Fields[i].IsNull) or (Length(Trim(table.Fields[i].AsString)) > 0) then temp := FormatDateTime('MM/DD/YYYY hh:nn:ss', table.Fields[i].AsDateTime) else //Put a valid default date and time temp := '01/01/2000 00:00:00'; end; ftTime: begin if (not table.Fields[i].IsNull) or (Length(Trim(table.Fields[i].AsString)) > 0) then temp := FormatDateTime ('hh:nn:ss', table.Fields[i].AsDateTime) else //Put a valid default time temp := '00:00:00'; end; end; child1.appendChild(doc.createTextNode(temp)); end; table.Next; end; doc.save(xml+'.xml'); memo1.lines.Append(doc.xml); Result:=1; except on e:Exception do Result:=-1; end; end;
能否利用ADODataSet的克浓方法复制过来,就得靠你自己得努力啦!
var
i : Integer;
xml,temp : String;
begin
try
table.close;
table.open;
xml := table.TableName;
doc := CreateOleObject('Microsoft.XMLDOM')
as IXMLDomDocument;
//Set the root name of the xml
file as that of the table name.
//In this case "country"
root := doc.createElement(xml);
doc.appendchild(root);
//This while loop will go through the
entire table to generate the xml file
while not table.eof do begin
//adds the first level children , Records
child:= doc.createElement('Records');
root.appendchild(child);
for i:=0 to table.FieldCount-1 do begin
//adds second level children
child1 := doc.createElement
(table.Fields[i].FieldName);
child.appendchild(child1);
//Check field types
case TFieldType
(Ord(table.Fields[i].DataType)) of
ftString:
begin
if Table.Fields[i].AsString ='' then
temp :='null' //Put a default string
else
temp := table.Fields[i].AsString;
end; ftInteger, ftWord, ftSmallint:
begin
if Table.Fields[i].AsInteger > 0 then
temp := IntToStr(table.Fields[i].AsInteger)
else
temp := '0';
end; ftFloat, ftCurrency, ftBCD:
begin
if table.Fields[i].AsFloat > 0 then
temp := FloatToStr(table.Fields[i].AsFloat)
else
temp := '0';
end; ftBoolean:
begin
if table.Fields[i].Value then
temp:= 'True'
else
temp:= 'False';
end; ftDate:
begin
if (not table.Fields[i].IsNull) or
(Length(Trim(table.Fields[i].AsString))
> 0) then
temp := FormatDateTime('MM/DD/YYYY',
table.Fields[i].AsDateTime)
else
//put a valid default date
temp:= '01/01/2000';
end; ftDateTime:
begin
if (not table.Fields[i].IsNull) or
(Length(Trim(table.Fields[i].AsString))
> 0) then
temp := FormatDateTime('MM/DD/YYYY hh:nn:ss',
table.Fields[i].AsDateTime)
else
//Put a valid default date and time
temp := '01/01/2000 00:00:00';
end; ftTime:
begin
if (not table.Fields[i].IsNull) or
(Length(Trim(table.Fields[i].AsString))
> 0) then
temp := FormatDateTime
('hh:nn:ss',
table.Fields[i].AsDateTime)
else
//Put a valid default time
temp := '00:00:00';
end;
end;
child1.appendChild(doc.createTextNode(temp));
end;
table.Next;
end;
doc.save(xml+'.xml');
memo1.lines.Append(doc.xml);
Result:=1;
except
on e:Exception do
Result:=-1;
end;
end;