XML:未安装 MSXML [英] XML: MSXML Not Installed

查看:9
本文介绍了XML:未安装 MSXML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码获取网站的 RSS.此代码适用于我的计算机和许多其他计算机.但在某些计算机(Windows XP 或 7)中,我收到此错误:MSXML Not Installed

Using this code to get rss of a site. This code works fine for my computer and many other computers. But in some computers (Windows XP or 7) I get this error: MSXML Not Installed

我该如何解决这个问题?怎么了?

代码如下:

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.

As you see I'm useing threads. So Synchronize was needed.

推荐答案

在 Windows 上,TXMLDocument 默认使用 MSXML,它使用 COM 对象.您的线程在加载 XML 之前没有调用 CoInitialize/Ex(),因此 COM 无法实例化 IXMLDocument 尝试在内部创建的任何 MSXML COM 对象(它尝试创建多个 COM 对象以发现实际安装的 MSXML 版本).您看到的错误消息意味着所有 MSXML COM 对象都无法实例化.

On Windows, TXMLDocument uses MSXML by default, which uses COM objects. Your thread is not calling CoInitialize/Ex() before loading the XML, so COM is not able to instantiate any of the MSXML COM objects that IXMLDocument attempts to create internally (it attempts to create multiple COM objects to discover which version of MSXML is actually installed). The error message you are seeing means all of the MSXML COM objects failed to instantiate.

您必须在每个访问 COM 对象的线程上下文中调用 CoInitialize/Ex(),例如:

You MUST call CoInitialize/Ex() in every thread context that accesses COM objects, eg:

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:

UPDATE: If you don't want to rely on this: you can use a different XML library of your choosing, you don't have to use MSXML:

使用文档对象模型

选择 XML 供应商

当您构建应用程序时,RAD Studio 使用默认的内置 XML 供应商 MSXML.

When you build an application, RAD Studio uses the default built-in XML vendor, MSXML.

如果您没有指定不同的 XML 供应商,您的应用程序在 Windows 以外的其他平台上不支持 XML,当您在其他平台上运行您的应用程序时,您会看到运行时异常.对于跨平台应用程序,OmniXML 是目前最好的选择,因为它显示出比 ADOM 更好的性能.

If you do not specify a different XML vendor, your application does not have XML support on other platforms than Windows, and you see a run-time exception when you run your application in other platforms. For cross-platform applications, OmniXML is currently the best choice, as it shows much better performance than ADOM.

要选择不同的 XML 供应商,添加参考到供应商的单位进入您使用 RTL XML 功能的单元,例如 TXMLDocument 类.如果您添加多个 XML 供应商单元,则引用的第一个单元将用作 XML 供应商.如果您需要覆盖此行为,请更改 DefaultDOMVendor 全局变量到您要使用的 XML 供应商的全局变量.

To choose a different XML vendor, add a reference to the unit of the vendor into the unit where you use the RTL XML features, such as the TXMLDocument class. If you add more than one XML vendor unit, the first unit referenced is used as XML vendor. If you need to override this behavior, change the value of the DefaultDOMVendor global variable to the global variable of the XML vendor that you want to use.

注意:您可以在 内置 XML 供应商列表 上面.

Note: You can look up the unit and the global variable of each XML vendor in the List of Built-in XML Vendors above.

当您使用 TXMLDocument组件,您可以使用其 DOMVendor 属性.当您更改 DOMVendor,使用该组件的单元被配置为使用指定的 XML 供应商,因此您无需更改单元引用或 DefaultDOMVendor 手动全局变量.

When you use the TXMLDocument component, you can choose an XML vendor using its DOMVendor property. When you change the value of DOMVendor, the unit that uses the component is configured to use the specified XML vendor, so that you do not need to change unit references or the DefaultDOMVendor global variable manually.

这篇关于XML:未安装 MSXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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