一个关于listbox读取xml的问题 一个listbox,一个TWebBrowser 一个XML 一个机构体Record想让listbox读取xml中的网站的数据,数据包括网站名称,地址。当点击listbox当中的一项的时候TWebBrowser可以打开对应的网站。因为要传递网址的数据,所以要加入一个结构体Record应该如何实现呢,主要就是在record读取xml的时候 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 分析xml将xml中每个节点的数据网站名称及地址放到listbox中,将其需要提交的数据保存到record中,当点击listbox的item的时候将对应的record中的数据post到当前item所对应的网站上。我理解的对么? 对的,但是我不知道怎么做,呵呵,delphi比较烂 在LISTBOX的ONCLICK事件 里写webbrowser.url..:=listbox... 不好意思不记得了。。看下2个控件的使用方法就知道了 ONListBoxClick(Sender:TObject)var str : String;begin str := ListBox1.Items[ListBox1.ItemIndex]; webbrowser1.navigate( str +'.asp); end; project1.xml<?xml version="1.0" encoding="gb2312" ?> <root> <item url="http://xx.xx.cn" name="1" data1="1" data2="1" /> <item url="http://xx.xx.cn" name="2" data1="1" data2="1" /> <item url="http://xx.xx.cn" name="3" data1="1" data2="1" /> <item url="http://xx.xx.cn" name="4" data1="1" data2="1" /> <item url="http://xx.xx.cn" name="5" data1="1" data2="1" /> <item url="http://xx.xx.cn" name="6" data1="1" data2="1" /> <item url="http://xx.xx.cn" name="7" data1="1" data2="1" /> </root>unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls,comobj;type PData = ^TData; TData = record url:string; data1:string; data2:string; end; TForm1 = class(TForm) Button1: TButton; ListBox1: TListBox; procedure Button1Click(Sender: TObject); procedure ListBox1Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } procedure clear; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}//--读取xml数据保存到listbox中procedure TForm1.Button1Click(Sender: TObject);var XMLDoc: TXMLDocument; iNode, tmpNode: IXMLNode; p:PData;begin //-- clear; XMLDoc:= TXMLDocument.Create(Owner); XMLDoc.FileName := ChangeFileExt(ParamStr(0),'.XML');//xml存放位置 XMLDoc.Active := True; iNode := XMLDoc.DocumentElement.ChildNodes.First; while iNode <> nil do begin new(p); p.url:= iNode.Attributes['url']; p.data1:=iNode.Attributes['data1']; p.data2:=iNode.Attributes['data2']; listbox1.Items.AddObject(iNode.Attributes['name'],TObject(p)); iNode := iNode.NextSibling; end; XMLDoc.Free;end;procedure TForm1.ListBox1Click(Sender: TObject);var p:PData; procedure post(url:string;p:PData);//将数据提交到指定的url接收页 var xmlHttp:Olevariant; responseText:Widestring; xmlStr:string; begin xmlStr:='<?xml version=''1.0'' encoding=''gb2312'' ?>'; xmlstr:=xmlstr+'<data data1='+p.data1+' data2='+p.data2+' />'; try xmlHttp:=CreateOleObject('Msxml2.XMLHTTP'); xmlHttp.open('POST',url,false); xmlHttp.setRequestHeader('CONTENT-TYPE','application/x-www-form-urlencoded'); xmlHttp.send(xmlStr); responseText:=xmlHttp.responseText; if xmlHttp.status='200' then begin //-- end else begin //-- end; except On E:Exception do //-- end; end;begin //--获得listbox的item保存的data数据 p:=PData(Pointer(self.ListBox1.Items.Objects[self.ListBox1.ItemIndex])); post(p.url,p);end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin clear;end;procedure TForm1.clear;var intI:Integer;begin for intI:=self.ListBox1.Items.Count-1 downto 0 do begin dispose(Pointer(self.ListBox1.Items.Objects[intI])); end; self.ListBox1.Items.Clear;end;end. hongqi162 太感谢你了,你编写的这个能不能给我发一个呢, hongqi162 有点一点没有明白,我想实现的是在项目中有一个TWebBrowser控件。,我想实现的是当点击listbox的时候可以使用TWebBrowser.Navigate的方式来让这个控件打开对应的网址,不知道您给我写的这个是不是实现这个功能的。我没有太看明白 求个控件..搜了半天找不到能满足要求的~谢谢~ delphi中颜色得表示 〖非常奇怪的OnCalcFields事件!!疑惑中求解!!〗 急需vclzip for bcb6的注册版,谁有发给我,兄弟感激不尽,100分送上! 打开再说吧 关于QuickRep,能否得到某张报表是否已经打印??? ado问题 程序员增值合定本中IntraWeb应用的例子的问题? 哪个函数可以调用出Windows的路径选择对话框,控件也可以 怎样编写动态创建的控件的事件响应? Locate 发现raize一个 重要的 bug
var
str : String;
begin
str := ListBox1.Items[ListBox1.ItemIndex];
webbrowser1.navigate( str +'.asp);
end;
project1.xml<?xml version="1.0" encoding="gb2312" ?>
<root>
<item url="http://xx.xx.cn" name="1" data1="1" data2="1" />
<item url="http://xx.xx.cn" name="2" data1="1" data2="1" />
<item url="http://xx.xx.cn" name="3" data1="1" data2="1" />
<item url="http://xx.xx.cn" name="4" data1="1" data2="1" />
<item url="http://xx.xx.cn" name="5" data1="1" data2="1" />
<item url="http://xx.xx.cn" name="6" data1="1" data2="1" />
<item url="http://xx.xx.cn" name="7" data1="1" data2="1" />
</root>
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls,comobj;type
PData = ^TData;
TData = record
url:string;
data1:string;
data2:string;
end;
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
procedure clear;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}//--读取xml数据保存到listbox中
procedure TForm1.Button1Click(Sender: TObject);
var
XMLDoc: TXMLDocument;
iNode, tmpNode: IXMLNode;
p:PData;
begin
//--
clear;
XMLDoc:= TXMLDocument.Create(Owner);
XMLDoc.FileName := ChangeFileExt(ParamStr(0),'.XML');//xml存放位置
XMLDoc.Active := True;
iNode := XMLDoc.DocumentElement.ChildNodes.First;
while iNode <> nil do
begin
new(p);
p.url:= iNode.Attributes['url'];
p.data1:=iNode.Attributes['data1'];
p.data2:=iNode.Attributes['data2'];
listbox1.Items.AddObject(iNode.Attributes['name'],TObject(p));
iNode := iNode.NextSibling;
end;
XMLDoc.Free;
end;procedure TForm1.ListBox1Click(Sender: TObject);
var
p:PData;
procedure post(url:string;p:PData);//将数据提交到指定的url接收页
var
xmlHttp:Olevariant;
responseText:Widestring;
xmlStr:string;
begin
xmlStr:='<?xml version=''1.0'' encoding=''gb2312'' ?>';
xmlstr:=xmlstr+'<data data1='+p.data1+' data2='+p.data2+' />';
try
xmlHttp:=CreateOleObject('Msxml2.XMLHTTP');
xmlHttp.open('POST',url,false);
xmlHttp.setRequestHeader('CONTENT-TYPE','application/x-www-form-urlencoded');
xmlHttp.send(xmlStr);
responseText:=xmlHttp.responseText;
if xmlHttp.status='200' then
begin
//--
end
else
begin
//--
end;
except
On E:Exception do
//--
end;
end;
begin
//--获得listbox的item保存的data数据
p:=PData(Pointer(self.ListBox1.Items.Objects[self.ListBox1.ItemIndex]));
post(p.url,p);
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
clear;
end;procedure TForm1.clear;
var
intI:Integer;
begin
for intI:=self.ListBox1.Items.Count-1 downto 0 do
begin
dispose(Pointer(self.ListBox1.Items.Objects[intI]));
end;
self.ListBox1.Items.Clear;
end;end.