如何将数据库查询的行转换为XML文件? [英] How to convert rows of a database query to a XML file?

查看:126
本文介绍了如何将数据库查询的行转换为XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有没有任何组件或库可以做到这一点?如果没有,那么构建DB2XML转换器的最佳方法是什么?



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

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

解决方案

可以使用 TDataSetProvider 组件填充 TClientDataSet 与TDataSet内容,然后使用 <$ c $



尝试此示例


$ b $

 过程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
如果不是DataSet.Active然后
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;


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.

解决方案

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;

这篇关于如何将数据库查询的行转换为XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆