Microsoft VBScript 运行时错误 错误 ''800a01a8'' 缺少对象: ''xmlDoc.documentElement'' /work/Menu.asp,行 80
找到相关代码如下:
复制代码 代码如下:
Set xmlDoc=Server.CreateObject("MicroSoft.XmlDom") xmlDoc.async = false xmlDoc.load(Server.MapPath("Menu.xml")) Set root = xmlDoc.documentElement.selectSingleNode("//index")
function CreateDOMDocument: IXMLDOMDocument;
var doc :IXMLDOMDocument2;
begin
doc := TryObjectCreate([CLASS_DOMDocument60,CLASS_DOMDocument40,CLASS_DOMDocument30,CLASS_DOMDocument26,msxml.CLASS_DOMDocument]) as IXMLDOMDocument2;
if not Assigned(doc) then
raise DOMException.Create(SMSDOMnotinstalled);
doc.setProperty('AllowXsltScript',true); // Allow XSLT scripts!!
Result := doc;
end;
显然这远非令人满意 – 所以如何在不修改库代码的情况下访问IXMLDOMDocument2对象?
解决方法
您可以通过MSXMLDOMDocumentCreate变量覆盖create函数:
unit Unit27;
interface
uses
xmldoc,xmlintf,msxml,msxmldom,Forms,SysUtils,ActiveX,ComObj,XmlDom,XmlConst,Windows,Messages,Classes,Controls,StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TryObjectCreate(const GuidList: array of TGuid): IUnkNown;
var
I: Integer;
Status: HResult;
begin
Status := S_OK;
for I := Low(GuidList) to High(GuidList) do
begin
Status := CoCreateInstance(GuidList[I],nil,CLSCTX_INPROC_SERVER or
CLSCTX_LOCAL_SERVER,Idispatch,Result);
if Status = S_OK then Exit;
end;
OleCheck(Status);
end;
function CreateDOMDocument2: IXMLDOMDocument;
var
Doc2 : IXMLDOMDocument2;
begin
Doc2 := TryObjectCreate([CLASS_DOMDocument60,msxml.CLASS_DOMDocument]) as IXMLDOMDocument2;
if not Assigned(Doc2) then
raise DOMException.Create(SMSDOMnotinstalled);
Doc2.setProperty('AllowXsltScript',true);
Result := Doc2;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Doc : IXMLDocument;
begin
Doc := TXMLDocument.Create(nil);
Doc.LoadFromFile('c:\temp\test.xml');
end;
initialization
MSXMLDOMDocumentCreate := CreateDOMDocument2;
end.