XML:未安装MSXML
使用此代码获取站点的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; 如你所见,我正在使用线程.所以需要Synchronize.
在Windows上,TXMLDocument默认使用MSXML,它使用COM对象.您的线程在加载XML之前没有调用CoInitialize / Ex(),因此COM无法实例化IXMLDocument在内部尝试创建的任何MSXML COM对象(它尝试创建多个COM对象以发现实际上哪个版本的MSXML安装).您看到的错误消息表示所有MSXML COM对象都无法实例化.
您必须在访问COM对象的每个线程上下文中调用CoInitialize / Ex(),例如: 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; 更新:如果您不想依赖于此:您可以使用您选择的其他XML库,您不必使用MSXML: Using the Document Object Model
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |