Sky*_*Sky 9 xml delphi xml-parsing
使用此代码获取站点的rss.此代码适用于我的计算机和许多其他计算机.但在某些计算机(Windows XP或7)中,我收到此错误:MSXML未安装
我该如何解决这个问题?怎么了?
这是代码:
procedure My_Thread.Execute;
var
http : tidhttp;
strm : tmemorystream;
str,sTitle, sDec ,er : string;
StartItemNode : IXMLNode;
ANode : IXMLNode;
XMLDoc : IXMLDocument;
begin
http := tidhttp.Create();
strm := tmemorystream.Create;
try
http.Get('http://www.sample.com/rss.xml',strm); //Download the RSS file
SetString(str,PANSIChar(strm.Memory),strm.Size);
XMLDoc := LoadXMLData(str);
StartItemNode := XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('item');
ANode := StartItemNode;
i := 0;
repeat
inc(i);
sTitle := ANode.ChildNodes['title'].Text;
sDec := ANode.ChildNodes['description'].Text;
Synchronize(procedure begin //Synchronize? I'm using threads
case I of
1: begin
main_frm.edit1.text := sTitle; //main_frm is my form
main_frm.edit2.text := sDec;
end;
2: begin
main_frm.edit3.text := sTitle;
main_frm.edit4.text := sDec;
end;
3: begin
main_frm.edit5.text := sTitle;
main_frm.edit6.text := sDec;
end;
end;
ANode := ANode.NextSibling;
end);
until ANode = nil;
http.Free;
strm.Free;
except
on E: Exception do
begin
er := e.Message;
Synchronize(procedure begin
ShowMessage(er);
end);
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
如你所见,我正在使用线程.所以Synchronize
需要.
Rem*_*eau 27
在Windows上,TXMLDocument
默认情况下使用MSXML,它使用COM对象.您的线程CoInitialize/Ex()
在加载XML之前没有调用,因此COM无法实例化任何IXMLDocument
尝试在内部创建的MSXML COM对象(它尝试创建多个COM对象以发现实际安装了哪个版本的MSXML).您看到的错误消息表示所有MSXML COM对象都无法实例化.
您必须调用CoInitialize/Ex()
访问COM对象的每个线程上下文,例如:
procedure My_Thread.Execute;
var
...
begin
CoInitialize(nil);
try
...
XMLDoc := LoadXMLData(str);
try
...
finally
// Since CoInitialize() and CoUninitialize() are being called in the same
// method as local COM interface variables, it is very important to release
// the COM interfaces before calling CoUninitialize(), do not just let them
// release automatically when they go out of scope, as that will be too late...
StartItemNode := nil;
ANode := nil;
XMLDoc := nil;
end;
...
finally
CoUninitialize;
end;
end;
Run Code Online (Sandbox Code Playgroud)
更新:如果您不想依赖于此:您可以使用您选择的其他XML库,您不必使用MSXML:
选择XML供应商
构建应用程序时,RAD Studio使用默认的内置XML供应商MSXML.
如果未指定其他XML供应商,则应用程序在Windows之外的其他平台上不支持XML,并且在其他平台上运行应用程序时会看到运行时异常.对于跨平台应用程序,OmniXML目前是最佳选择,因为它显示出比ADOM更好的性能.
要选择其他XML供应商,请将供应商单元的引用添加到使用RTL XML功能的单元中,例如
TXMLDocument
类.如果添加多个XML供应商单元,则引用的第一个单元将用作XML供应商.如果需要覆盖此行为,请将DefaultDOMVendor
全局变量的值更改为要使用的XML供应商的全局变量.注意:您可以在上面的内置 XML供应商列表中查找每个XML供应商的单元和全局变量.
使用
TXMLDocument
组件时,可以使用其DOMVendor
属性选择XML供应商.更改值时DOMVendor
,使用该组件的单元配置为使用指定的XML供应商,因此您无需DefaultDOMVendor
手动更改单元引用或全局变量.
归档时间: |
|
查看次数: |
10714 次 |
最近记录: |