怎么把查询出来的结果生成XML形式的文件?

jerrybao 2003-12-13 07:44:58
怎么把查询出来的结果生成XML形式的文件?
...全文
121 点赞 收藏 14
写回复
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
line 2004-03-12
up
回复
horse_h 2004-01-18
ding
回复
S海鸥 2004-01-05
study
回复
深宇 2004-01-05
ClientDataSet.XMLData
回复
dzjcsoft 2003-12-15
ClientDataSet.SaveToFile的。XML在IE中打不开。
回复
hiflower 2003-12-14
ClientDataSet.SaveToFile(...)
回复
halfdream 2003-12-14
我说另外三种方法.
1,使用TDataSetProvider和TClientDataset,TClientDataSet能把查询结果导出成XML.

2,使用TADODATASET也可以直接导出结果为XML..

3,SQL SERVER 2000 提供了直接查询得XML文档的SQL语法,
比如. select * from xxx for xml auto,具体看SQL SERVER 2000帮助,
不过要读出这个XML文档,必须使用ADO2.6以上的调用.
网上找得着例子..

第1种用法,是最轻捷的,DELPHI里面相应提供了相关的好几个组件,例子,还有XSL文档.
按查询速度,应该是第3种最快..



回复
zhoutian618 2003-12-14
同意楼上的方法。

用SAVETOFILE然后指定文件格式为.XLS
回复
MartinWang 2003-12-13
一个自己做的例子:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, StdCtrls, Buttons, msxmldom, XMLDoc, ComCtrls,
ImgList, DB, ADODB, ExtCtrls;

type
TForm1 = class(TForm)
XMLDocument1: TXMLDocument;
ListView1: TListView;
OpenDialog1: TOpenDialog;
ADOQuery1: TADOQuery;
Panel1: TPanel;
BitBtn2: TBitBtn;
BitBtn1: TBitBtn;
BitBtn3: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
procedure add;
procedure AddFromQuery(id,name,cha:string);
public
{ Public declarations }
XmlFile:WideString;
end;


var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);


begin
// XMLDocument1.LoadFromFile(PathString(AppPath)+'wangp.xml');
XmlFile:=ExtractFileName(Application.ExeName+'\wangp.xml');
XmlDocument1.FileName:=XmlFile;
XMLDocument1.Active:=true;
add;
end;

procedure TForm1.add;
var Node:IXMLNode;
NodeList:IXMLNodeList;
i,m,j:Integer;
lst:TListItem;
begin
ListView1.Items.Clear;
Node:=XMLDocument1.DocumentElement;//
ListView1.Items.BeginUpdate;
NodeList:=Node.ChildNodes;
for i:=0 to NodeList.Count-1 do
begin
lst:=ListView1.Items.Add;
Node:=NodeList.Nodes[i].ChildNodes.Nodes[0];
lst.Caption:='';
for j:=0 to NodeList.Nodes[i].ChildNodes.Count-1 do
begin
lst.SubItems.Add(NodeList.Nodes[i].ChildNodes.Nodes[j].GetText());
end;
lst.ImageIndex:=0;
end;
ListView1.Items.EndUpdate;
Node:=XMLDocument1.CreateElement('price','pri');
Node.

end;
procedure TForm1.AddFromQuery(id,name,cha:string);
var Node,SubNode:IXMLNode;
begin
Node:=XMLDocument1.DocumentElement;
Node:=Node.AddChild(WideString('Tip'));
SubNode:=Node.AddChild(WideString('userid'));
SubNode.SetText(id);
SubNode:=Node.AddChild(WideString('name'));
SubNode.SetText(name);
SubNode:=Node.AddChild(WideString('cha'));
SubNode.SetText(cha);
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var userid,name,cha:string;
begin
// AddFromQuery('wangp','slfj','22222.00');
AdoQuery1.Close;
AdoQuery1.Parameters.ParamByName('set').Value:='21101';
AdoQuery1.Open;
while not AdoQuery1.Eof do
begin
userid:=AdoQuery1.FieldByName('User_id').AsString;
name:=AdoQuery1.FieldByName('User_name').AsString;
cha:=AdoQuery1.FieldByName('total_cha').AsString;
AddFromQuery(userid,name,cha);
AdoQuery1.Next;
end;
XMLDocument1.SaveToFile(XMlFile);
BitBtn1Click(self);
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
var Node:IXMLNode;
i:Integer;
begin
i:=ListView1.Selected.Index;
Node:=XMLDocument1.DocumentElement;
Node.ChildNodes.Delete(i);
ListView1.Items.Delete(i);
XMLDocument1.SaveToFile(XMlFile);

end;

end.
回复
eastliangliang 2003-12-13
来个简单的,不过要是想自己显示记录集,需要自己定义标签格式,再定义,好像是XSLT,楼主可以看看相关文章。
procedure TForm1.Button1Click(Sender: TObject);
var
myxmldoc:Txmldocument;
html,head,title,body:Ixmlnode;
begin
myxmldoc := Txmldocument.Create(self);
myxmldoc.Active := true;
html := myxmldoc.AddChild('Html');
head := html.AddChild('head');
title := head.AddChild('title');
title.Text := 'this is title';
body := html.AddChild('body');
body.Text := 'Hello world';
showmessage(myxmldoc.XML.Text);
myxmldoc.SaveToFile('c:\a.html');
end;
要uses 几个东东XMLDoc, XMLIntf, xmldom, msxmldom,可能有的没用。:)
回复
cutelocust 2003-12-13
如果你用ADO的话可以这样
AdoQuery1.SaveToFile('filename', pfXML);
或者
ADodataset1.SaveToFile('filename', pfXML);
或者
adotable1.SaveToFile('filename', pfXML);
回复
eastliangliang 2003-12-13
你需要自己定义显示格式,然后用Delphi自带的控件就可以搞定。
给你一段代码,比较简单的XML操作,先入个门吧,难的我也不会,正在研究中:)
我用的是D6,不知D7的XML控件有没有改进,我是觉得D6对XML的操作不如C#的功能强大,可能我还不会用吧。
只是单层的树,你自己发挥吧:)
unit UnitArrayWithXML;

interface

uses
Mtsobj, Mtx, ComObj, ActiveX, StdVcl,XMLDoc, XMLIntf,
xmldom, msxmldom, Classes;

type
TArrayWithXML = class
private
Owner:TComponent;
myxmldoc:Txmldocument;
myxmlNode:Ixmlnode;
insertnode:Ixmlnode;
public
procedure Add(const fieldName, fieldProperty, fieldValue: WideString); safecall;
procedure GetByIndex(index: Integer; out fieldName, fieldProperty,
fieldValue: WideString); safecall;
procedure GetByFieldName(const fieldName: WideString;
out fieldProperty, fieldValue:WideString) safecall;
procedure SetValue(const inData: WideString); safecall;
function GetValue: WideString; safecall;
function GetNodeCount: Integer; safecall;
procedure Clear; safecall;
constructor Create;
destructor Destroy;override;
end;
implementation
uses ComServ, SysUtils;

procedure TArrayWithXML.Add(const fieldName, fieldProperty, fieldValue: WideString);
begin
//if fieldValue = '' then exit;
try
self.insertnode := self.myxmlNode.AddChild(LowerCase(fieldName));
if fieldvalue <> '' then
begin
self.insertnode.Text := fieldValue;
self.insertnode.Attributes['fieldProperty'] := fieldProperty;
end
else begin
self.insertnode.Text := 'Null';
self.insertnode.Attributes['fieldProperty'] := 'int';
end;
except
raise exception.Create('Error in Add to XML!向XML添加数据错误!');
end;
end;

procedure TArrayWithXML.GetByIndex(index: Integer; out fieldName, fieldProperty,
fieldValue: WideString);
begin
try
self.insertnode := self.myxmlNode.ChildNodes.Get(index);
fieldName := self.insertnode.NodeName;
fieldValue := self.insertnode.NodeValue;
if self.insertnode.HasAttribute('fieldProperty') then
fieldProperty := self.insertnode.Attributes['fieldProperty'];
except
raise exception.Create('Error in GetByIndex!从XML中读数据错误!');
end;
end;

procedure TArrayWithXML.GetByFieldName(
const fieldName: WideString;out fieldProperty, fieldValue:WideString);
begin
fieldValue := self.myxmlNode.ChildNodes[LowerCase(fieldName)].Text;
if self.myxmlNode.HasAttribute('fieldProperty') then
fieldProperty := self.myxmlNode.ChildNodes[LowerCase(fieldName)].Attributes['fieldProperty'];
end;

procedure TArrayWithXML.SetValue(const inData: WideString);
begin
try
self.myxmldoc.Active := false;
self.myxmldoc.XML.Text := inData;
self.myxmldoc.Active := true;
myxmlnode := myxmldoc.DocumentElement;
except
raise exception.Create('Error in SetValue!给XML赋值错误!');
end;
end;

function TArrayWithXML.GetValue: WideString;
begin
result := self.myxmldoc.XML.Text;
end;

function TArrayWithXML.GetNodeCount: Integer;
begin
result := myxmlnode.ChildNodes.Count;
end;

destructor TArrayWithXML.Destroy;
begin
Owner.Free;
inherited;
end;

constructor TArrayWithXML.Create;
begin
inherited;
Owner := TComponent.Create(nil);
self.myxmldoc := Txmldocument.Create(self.Owner);
with myxmldoc.XML do
begin
add('<?xml version="1.0" encoding="gb2312"?>');
add('<VariantArray></VariantArray>');
end;
myxmldoc.Active := true;
myxmlnode := myxmldoc.DocumentElement;
end;

procedure TArrayWithXML.Clear;
begin
self.myxmldoc.XML.Clear;
with myxmldoc.XML do
begin
add('<?xml version="1.0" encoding="gb2312"?>');
add('<VariantArray></VariantArray>');
end;
myxmldoc.Active := true;
myxmlnode := myxmldoc.DocumentElement;
end;

end.
回复
jerrybao 2003-12-13
象网页一样显示出来
回复
eastliangliang 2003-12-13
生成XML形式的文件用来干吗?
回复
发动态
发帖子
网络通信/分布式开发
创建于2007-08-02

1565

社区成员

Delphi 网络通信/分布式开发
申请成为版主
社区公告
暂无公告