How to convert rows of a database query to a XML file?

时光怂恿深爱的人放手 提交于 2019-12-21 05:41:16

问题


I am developing a Delphi application that needs to pick up the rows from a period of work and convert them to a single XML file in order to upload to a 3rd party web-service.

Is there any component or library available to do that? If not, what is the best approach of code to build that DB2XML conversor?

I noticed that most XML questions are about how to convert it to another type of data.

Note: the database will be MySQL or Firebird.


回答1:


You can use the TDataSetProvider component to fill a TClientDataSet with the TDataSet contents and then use the SaveToFile method to create the xml file.

Try this sample

procedure DataSetToXML(DataSet  : TDataSet; const FileName:string);
var
 LProvider : TDataSetProvider;
 LClient   : TClientDataSet;
begin
   LProvider:=TDataSetProvider.Create(nil);
   try
     LProvider.DataSet:=DataSet;
     LClient:=TClientDataSet.Create(nil);
     try
       DataSet.DisableControls;
       try
        if not DataSet.Active then
          DataSet.Active:=True;
        LClient.SetProvider(LProvider);
        LClient.Active:=True;
        LClient.SaveToFile(FileName, dfXMLUTF8);
       finally
         DataSet.EnableControls;
       end;
     finally
       LClient.Free;
     end;
   finally
     LProvider.Free;
   end;
end;



回答2:


You could put your table in a TClientDataSet, and then export as XML using the XMLData property.



来源:https://stackoverflow.com/questions/12150775/how-to-convert-rows-of-a-database-query-to-a-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!