使用最近发现的内置单元,可以用一种更方便快捷的方式读写xml文件。
参考:http://wiki.freepascal.org/XML_Tutorial/zh_CN
uses
...
Laz2_DOM, laz2_XMLRead, laz2_XMLWrite
...
// 导出选中表 procedure TForm_data_outport.Button2Click(Sender: TObject); var Doc: TXMLDocument; // variable to document RootNode, TableNode, RecNode, FieldNode: TDOMNode; // variable to nodes i, iField: integer; sTblEn, sTblCn, sql, sFldName, sFldValue: string; begin SaveDialog1.Filter:='XML格式文件(*.xml)'; if SaveDialog1.Execute then begin fnXml := SaveDialog1.FileName; if pos('.XML', uppercase(fnXml)) > 0 then fnXml := stringreplace(fnXml, '.xml', '',[rfIgnoreCase]); end; Doc := TXMLDocument.Create; // Create a root node RootNode := Doc.CreateElement('ROOT'); Doc.Appendchild(RootNode); // save root node for i := 0 to cLBoxTblNameCn.Items.Count - 1 do begin if cLBoxTblNameCn.Checked[i] = true then begin sTblEn := LBoxTblNameEn.Items.Strings[i]; sTblCn := cLBoxTblNameCn.Items.Strings[i]; // 增加表 TableNode := Doc.CreateElement('table'); TDOMElement(TableNode).SetAttribute('name', sTblEn); TDOMElement(TableNode).SetAttribute('describ', sTblCn); RootNode.Appendchild(TableNode); sql := 'select * from ' + sTblEn; ZROQuery.Close; ZROQuery.SQL.Clear; ZROQuery.SQL.Add(sql); ZROQuery.Open; memo1.Append('表"'+sTblCn+'"导出记录数量:' + intToStr(ZROQuery.RecordCount)); pBar.Max := ZROQuery.RecordCount; // 循环记录 while not ZROQuery.EOF do begin pBar.Position:=ZROQuery.RecNo; RecNode := Doc.CreateElement('record'); TableNode.Appendchild(RecNode); // 循环字段 for iField := 0 to ZROQuery.FieldCount - 1 do begin FieldNode := Doc.CreateElement('field'); sFldName := ZROQuery.Fields[iField].FieldName; sFldValue := ZROQuery.Fields[iField].AsString; TDOMElement(FieldNode).SetAttribute(sFldName, sFldValue); //TDOMElement(FieldNode). RecNode.Appendchild(FieldNode); end; ZROQuery.Next; end; pBar.Position:=0; end; end; writeXMLFile(Doc, (fnXml + '.xml')); // write to XML Doc.Free; end;