如和把纪录集存成xml(100分)

  • 主题发起人 主题发起人 zht_66407
  • 开始时间 开始时间
Z

zht_66407

Unregistered / Unconfirmed
GUEST, unregistred user!
在sql server上可以rs.save "e:/aaaa.xml",adPersistXML 保存成功
而在sybase上却通不过为何?应该与数据库无关吧
 
可能有关系,主要是看数据库是否支持xml。
 
这里有个单元,不知道能否有帮助:
unit DS2XML;
interface
uses
Classes, DB;
procedure DatasetToXML(Dataset: TDataSet;
FileName: string);
implementation
uses
SysUtils;
var
SourceBuffer: PChar;

procedure WriteString(Stream: TFileStream;
s: string);
begin

StrPCopy(SourceBuffer, s);
Stream.Write(SourceBuffer[0], StrLen(SourceBuffer));
end;

procedure WriteFilebegin
(Stream: TFileStream;
Dataset: TDataSet);

function XMLFieldType(fld: TField): string;
begin

case fld.DataType of
ftString: Result := '"string" WIDTH="' + IntToStr(fld.Size) + '"';
ftSmallint: Result := '"i4"';
//??
ftInteger: Result := '"i4"';
ftWord: Result := '"i4"';
//??
ftBoolean: Result := '"boolean"';
ftAutoInc: Result := '"i4" SUBTYPE="Autoinc"';
ftFloat: Result := '"r8"';
ftCurrency: Result := '"r8" SUBTYPE="Money"';
ftBCD: Result := '"r8"';
//??
ftDate: Result := '"date"';
ftTime: Result := '"time"';
//??
ftDateTime: Result := '"datetime"';
else

end;

if fld.Required then

Result := Result + ' required="true"';
if fld.ReadOnly then

Result := Result + ' readonly="true"';
end;

var
i: Integer;
begin

WriteString(Stream, '<?xml version="1.0" standalone="yes"?><!-- Generated by SMExport --> ' +
'<DATAPACKET Version="2.0">');
WriteString(Stream, '<METADATA><FIELDS>');

{write th metadata}
with Datasetdo

for i := 0 to FieldCount - 1do

begin

WriteString(Stream, '<FIELD attrname="' +
Fields.FieldName +
'" fieldtype=' +
XMLFieldType(Fields) +
'/>');
end;

WriteString(Stream, '</FIELDS>');
WriteString(Stream, '<PARAMS DEFAULT_ORDER="1" PRIMARY_KEY="1" LCID="1033"/>');
WriteString(Stream, '</METADATA><ROWDATA>');
end;

procedure WriteFileEnd(Stream: TFileStream);
begin

WriteString(Stream, '</ROWDATA></DATAPACKET>');
end;

procedure WriteRowStart(Stream: TFileStream;
IsAddedTitle: Boolean);
begin

if not IsAddedTitle then

WriteString(Stream, '<ROW');
end;

procedure WriteRowEnd(Stream: TFileStream;
IsAddedTitle: Boolean);
begin

if not IsAddedTitle then

WriteString(Stream, '/>');
end;

procedure WriteData(Stream: TFileStream;
fld: TField;
AString: ShortString);
begin

if Assigned(fld) and (AString <> '') then

WriteString(Stream, ' ' + fld.FieldName + '="' + AString + '"');
end;

function GetFieldStr(Field: TField): string;

function GetDig(i, j: Word): string;
begin

Result := IntToStr(i);
while (Length(Result) < j)do

Result := '0' + Result;
end;

var
Hour, Min, Sec, MSec: Word;
begin

case Field.DataType of
ftBoolean: Result := UpperCase(Field.AsString);
ftDate: Result := FormatDateTime('yyyymmdd', Field.AsDateTime);
ftTime: Result := FormatDateTime('hhnnss', Field.AsDateTime);
ftDateTime:
begin

Result := FormatDateTime('yyyymmdd', Field.AsDateTime);
DecodeTime(Field.AsDateTime, Hour, Min, Sec, MSec);
if (Hour <> 0) or (Min <> 0) or (Sec <> 0) or (MSec <> 0) then

Result := Result + 'T' + GetDig(Hour, 2) + ':' + GetDig(Min,
2) + ':' + GetDig(Sec, 2) + GetDig(MSec, 3);
end;

else

Result := Field.AsString;
end;

end;

procedure DatasetToXML(Dataset: TDataSet;
FileName: string);
var
Stream: TFileStream;
bkmark: TBookmark;
i: Integer;
begin

Stream := TFileStream.Create(FileName, fmCreate);
SourceBuffer := StrAlloc(1024);
WriteFilebegin
(Stream, Dataset);

with DataSetdo

begin

DisableControls;
bkmark := GetBookmark;
First;

{write a title row}
WriteRowStart(Stream, True);
for i := 0 to FieldCount - 1do

WriteData(Stream, nil, Fields.DisplayLabel);
{write the end of row}
WriteRowEnd(Stream, True);

while (not EOF)do

begin

WriteRowStart(Stream, False);
for i := 0 to FieldCount - 1do

WriteData(Stream, Fields, GetFieldStr(Fields));
{write the end of row}
WriteRowEnd(Stream, False);

Next;
end;

GotoBookmark(bkmark);
EnableControls;
end;

WriteFileEnd(Stream);
Stream.Free;
StrDispose(SourceBuffer);
end;

end.


//Beispiel, Example:

uses DS2XML;

procedure TForm1.Button1Click(Sender: TObject);
begin

DatasetToXML(Table1, 'test.xml');
end;
 
已经搞定,记录集参数问题
 
ClientDataSet1.SaveToFile('c:/a.xml',[])
 
zht_66407,
怎么做的?
 
把adoquery1的数据集给clientdataset1,由clientdataset1.savetofile即可
 
后退
顶部