如何将数据库查询的行转换为XML文件?

时间:2021-02-09 23:38:00

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.

我正在开发一个Delphi应用程序,它需要从一段时间内获取行并将它们转换为单个XML文件,以便上传到第三方Web服务。

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

有没有可用的组件或库?如果没有,那么构建DB2XML转换器的最佳代码方法是什么?

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

我注意到大多数XML问题都是关于如何将其转换为另一种类型的数据。

Note: the database will be MySQL or Firebird.

注意:数据库将是MySQL或Firebird。

2 个解决方案

#1


7  

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.

您可以使用TDataSetProvider组件使用TDataSet内容填充TClientDataSet,然后使用SaveToFile方法创建xml文件。

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


0  

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

您可以将表放在TClientDataSet中,然后使用XMLData属性导出为XML。

#1


7  

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.

您可以使用TDataSetProvider组件使用TDataSet内容填充TClientDataSet,然后使用SaveToFile方法创建xml文件。

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


0  

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

您可以将表放在TClientDataSet中,然后使用XMLData属性导出为XML。