用微软的MSXML服务:Microsoft XML Core Services (MSXML) 4.0 Microsoft® XML Core Services (MSXML) 4.0, formerly known as the Microsoft XML Parser, allows customers to build high-performance XML-based applications that provide a high degree of interoperability with other applications that adhere to the XML 1.0 standard. Among the core services MSXML 4.0 provides is developer support for the following: The Document Object Model (DOM), a standard library of application programming interfaces (APIs) for accessing XML documents. The XML Schema definition language (XSD), a current W3C standard for using XML to create XML Schemas. XML Schemas can be used to validate other XML documents. The Schema Object Model (SOM), an additional set of APIs for accessing XML Schema documents programmatically. Extensible Stylesheet Language Transformations (XSLT) 1.0, a current W3C XML style sheet language standard. XSLT is recommended for transforming XML documents. The XML Path Language (XPath) 1.0, a current W3C XML standard used by XSLT and other XML programming vocabularies to query and filter data stored in XML documents. The Simple API for XML (SAX), a programmatic alternative to DOM-based processing.
这是我以前写的一段代码,把Table1的表结构和数据生成一个XML文件,然后再用XML生成Table2。比较简单,深入学习的话还是看微软的帮助文档,写得很好。unit DataSetAndXML;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComObj, Grids, DBGrids, ExtCtrls, DB, DBTables, ShellAPI;type TForm1 = class(TForm) Panel1: TPanel; Panel2: TPanel; Button1: TButton; Button2: TButton; Button3: TButton; DBGrid1: TDBGrid; DBGrid2: TDBGrid; DataSource1: TDataSource; Table1: TTable; Table2: TTable; DataSource2: TDataSource; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } ExePath: string; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject); begin ExePath := ExtractFilePath(Application.ExeName); Table2.DatabaseName := ExePath; Table1.Open; end;procedure TForm1.Button1Click(Sender: TObject); var I: Integer; XMLDoc, ProcessingInstruction, DataSetElement, FieldDefsElement, FieldElement, RecordsElement, RecordElement: Variant; begin XMLDoc := CreateOleObject('Msxml2.DOMDocument'); // ProcessingInstruction ProcessingInstruction := XMLDoc.CreateProcessingInstruction('xml', 'version="1.0" encoding="GB2312"'); XMLDoc.AppendChild(ProcessingInstruction); // DataSet Element DataSetElement := XMLDoc.CreateElement('DataSet'); DataSetElement.SetAttribute('TableName', Table1.TableName); XMLDoc.AppendChild(DataSetElement); // FieldDefs Element FieldDefsElement := XMLDoc.CreateElement('FieldDefs'); DataSetElement.AppendChild(FieldDefsElement); // Field Element for I := 0 to Table1.FieldCount - 1 do begin FieldElement := XMLDoc.CreateElement('Field'); FieldElement.SetAttribute('FieldName', Table1.Fields[I].FieldName); FieldElement.SetAttribute('DataType', Table1.Fields[I].DataType); FieldElement.SetAttribute('Size', Table1.Fields[I].Size); FieldDefsElement.AppendChild(FieldElement); end; // Records Element RecordsElement := XMLDoc.CreateElement('Records'); DataSetElement.AppendChild(RecordsElement); // Record Element Table1.First; while not Table1.EOF do begin RecordElement := XMLDoc.CreateElement('Record'); RecordElement.SetAttribute('RecNo', Table1.RecNo); for I := 0 to Table1.FieldCount - 1 do RecordElement.SetAttribute(Table1.Fields[I].FieldName, Table1.Fields[I].AsString); RecordsElement.AppendChild(RecordElement); Table1.Next; end; // As string ShowMessage(XMLDoc.XML); // Save to file XMLDoc.Save(ExePath + ChangeFileExt(Table1.TableName, '.xml')); XMLDoc := Unassigned; end;procedure TForm1.Button2Click(Sender: TObject); begin ShellExecute(0, 'Open', PChar(ExePath + ChangeFileExt(Table1.TableName, '.xml')), '', '', SW_MAXIMIZE); end;procedure TForm1.Button3Click(Sender: TObject); var XMLDoc, DataSetElement, FieldElementList, FieldElement, RecordElementList, RecordElement: Variant; I, J: Integer; S1, S2, S3: string; begin XMLDoc := CreateOleObject('Msxml2.DOMDocument'); XMLDoc.Load(ExePath + ChangeFileExt(Table1.TableName, '.xml')); // FieldDefs Table2.Close; DataSetElement := XMLDoc.SelectSingleNode('/DataSet'); Table2.TableName := DataSetElement.GetAttribute('TableName'); Table2.FieldDefs.Clear; FieldElementList := XMLDoc.GetElementsByTagName('Field'); for I := 0 to FieldElementList.Length - 1 do begin FieldElement := FieldElementList.Item(I); S1 := FieldElement.GetAttribute('FieldName'); S2 := FieldElement.GetAttribute('DataType'); S3 := FieldElement.GetAttribute('Size'); Table2.FieldDefs.Add(S1, TFieldType(StrToInt(S2)), StrToInt(S3), False); end; Table2.CreateTable; Table2.Open; // Records RecordElementList := XMLDoc.GetElementsByTagName('Record'); for I := 0 to RecordElementList.Length - 1 do begin RecordElement := RecordElementList.Item(I); Table2.Append; for J := 0 to Table2.FieldCount - 1 do Table2.Fields[J].AsString := RecordElement.GetAttribute(Table2.Fields[J].FieldName); Table2.Post; end; XMLDoc := Unassigned; end;end.
Microsoft® XML Core Services (MSXML) 4.0, formerly known as the Microsoft XML Parser, allows customers to build high-performance XML-based applications that provide a high degree of interoperability with other applications that adhere to the XML 1.0 standard. Among the core services MSXML 4.0 provides is developer support for the following: The Document Object Model (DOM), a standard library of application programming interfaces (APIs) for accessing XML documents.
The XML Schema definition language (XSD), a current W3C standard for using XML to create XML Schemas. XML Schemas can be used to validate other XML documents.
The Schema Object Model (SOM), an additional set of APIs for accessing XML Schema documents programmatically.
Extensible Stylesheet Language Transformations (XSLT) 1.0, a current W3C XML style sheet language standard. XSLT is recommended for transforming XML documents.
The XML Path Language (XPath) 1.0, a current W3C XML standard used by XSLT and other XML programming vocabularies to query and filter data stored in XML documents.
The Simple API for XML (SAX), a programmatic alternative to DOM-based processing.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj, Grids, DBGrids, ExtCtrls, DB, DBTables,
ShellAPI;type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
Table2: TTable;
DataSource2: TDataSource;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
ExePath: string;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
ExePath := ExtractFilePath(Application.ExeName);
Table2.DatabaseName := ExePath;
Table1.Open;
end;procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
XMLDoc, ProcessingInstruction, DataSetElement, FieldDefsElement, FieldElement, RecordsElement, RecordElement: Variant;
begin
XMLDoc := CreateOleObject('Msxml2.DOMDocument'); // ProcessingInstruction
ProcessingInstruction := XMLDoc.CreateProcessingInstruction('xml', 'version="1.0" encoding="GB2312"');
XMLDoc.AppendChild(ProcessingInstruction); // DataSet Element
DataSetElement := XMLDoc.CreateElement('DataSet');
DataSetElement.SetAttribute('TableName', Table1.TableName);
XMLDoc.AppendChild(DataSetElement); // FieldDefs Element
FieldDefsElement := XMLDoc.CreateElement('FieldDefs');
DataSetElement.AppendChild(FieldDefsElement);
// Field Element
for I := 0 to Table1.FieldCount - 1 do
begin
FieldElement := XMLDoc.CreateElement('Field');
FieldElement.SetAttribute('FieldName', Table1.Fields[I].FieldName);
FieldElement.SetAttribute('DataType', Table1.Fields[I].DataType);
FieldElement.SetAttribute('Size', Table1.Fields[I].Size);
FieldDefsElement.AppendChild(FieldElement);
end; // Records Element
RecordsElement := XMLDoc.CreateElement('Records');
DataSetElement.AppendChild(RecordsElement);
// Record Element
Table1.First;
while not Table1.EOF do
begin
RecordElement := XMLDoc.CreateElement('Record');
RecordElement.SetAttribute('RecNo', Table1.RecNo);
for I := 0 to Table1.FieldCount - 1 do
RecordElement.SetAttribute(Table1.Fields[I].FieldName, Table1.Fields[I].AsString);
RecordsElement.AppendChild(RecordElement);
Table1.Next;
end; // As string
ShowMessage(XMLDoc.XML);
// Save to file
XMLDoc.Save(ExePath + ChangeFileExt(Table1.TableName, '.xml')); XMLDoc := Unassigned;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
ShellExecute(0, 'Open', PChar(ExePath + ChangeFileExt(Table1.TableName, '.xml')), '', '', SW_MAXIMIZE);
end;procedure TForm1.Button3Click(Sender: TObject);
var
XMLDoc, DataSetElement, FieldElementList, FieldElement, RecordElementList, RecordElement: Variant;
I, J: Integer;
S1, S2, S3: string;
begin
XMLDoc := CreateOleObject('Msxml2.DOMDocument'); XMLDoc.Load(ExePath + ChangeFileExt(Table1.TableName, '.xml')); // FieldDefs
Table2.Close;
DataSetElement := XMLDoc.SelectSingleNode('/DataSet');
Table2.TableName := DataSetElement.GetAttribute('TableName');
Table2.FieldDefs.Clear;
FieldElementList := XMLDoc.GetElementsByTagName('Field');
for I := 0 to FieldElementList.Length - 1 do
begin
FieldElement := FieldElementList.Item(I);
S1 := FieldElement.GetAttribute('FieldName');
S2 := FieldElement.GetAttribute('DataType');
S3 := FieldElement.GetAttribute('Size');
Table2.FieldDefs.Add(S1, TFieldType(StrToInt(S2)), StrToInt(S3), False);
end;
Table2.CreateTable;
Table2.Open; // Records
RecordElementList := XMLDoc.GetElementsByTagName('Record');
for I := 0 to RecordElementList.Length - 1 do
begin
RecordElement := RecordElementList.Item(I);
Table2.Append;
for J := 0 to Table2.FieldCount - 1 do
Table2.Fields[J].AsString := RecordElement.GetAttribute(Table2.Fields[J].FieldName);
Table2.Post;
end; XMLDoc := Unassigned;
end;end.