首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以在Delphi中将msxml.IXMLDOMNode转换为XmlIntf.IXMLNode吗?

我可以在Delphi中将msxml.IXMLDOMNode转换为XmlIntf.IXMLNode吗?
EN

Stack Overflow用户
提问于 2011-11-16 16:57:21
回答 1查看 3.3K关注 0票数 11

我已经将某些xml读取到一个msxml.IXMLDOMDocument对象中。但是,在我正在使用的应用程序接口中有一个实用程序方法,我想调用它,但它以XmlIntf.IXMLNode作为参数。

有没有一种简单的方法可以将IXMLDOMNode实例从我的文档转换成IXMLNode,这样我就可以将它传递给方法,而不必将它加载到TXmlDocument对象中?

到目前为止,我已经实现了这个解决方法:

代码语言:javascript
复制
function ConvertNode(const Node: IXMLDOMNode): IXMLNode;
var
  Document: IXMLDocument;
begin
  Document := NewXMLDocument;
  Document.LoadFromXML(Node.xml);
  Result := Document.DocumentElement;
end;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-16 18:53:52

您可以创建一个IXMLDocument实例并将其“附加”到您的IXMLDOMDocument。下面是一个小示例:

代码语言:javascript
复制
program msxmltest;

{$APPTYPE CONSOLE}

uses
  SysUtils, ActiveX,
  msxml, msxmldom,
  xmldom,
  XMLIntf, XMLDoc;

function MSDOMDocumentToDOMDocument(const MSDOMDocument: IXMLDOMDocument): IDOMDocument;
begin
  Result := TMSDOMDocument.Create(MSDOMDocument) as IDOMDocument;
end;

function MSDOMNodeToDOMNode(const MSDOMNode: IXMLDOMNode): IDOMNode;
const
  NodeClasses: array[ELEMENT_NODE..NOTATION_NODE] of TMSDOMNodeClass =
    (TMSDOMElement, TMSDOMAttr, TMSDOMText, TMSDOMCDataSection,
     TMSDOMEntityReference, TMSDOMEntity, TMSDOMProcessingInstruction,
     TMSDOMComment, TMSDOMDocument, TMSDOMDocumentType, TMSDOMDocumentFragment,
     TMSDOMNotation);
begin
  Result := NodeClasses[MSDOMNode.nodeType].Create(MSDOMNode) as IDOMNode;
end;

function MSDOMNodeToXMLNode(const MSDOMNode: IXMLDOMNode; var OwnerDocument: IXMLDocument): IXMLNode;
begin
  Result := nil;
  if not Assigned(MSDOMNode) then
    Exit;
  if not Assigned(OwnerDocument) then
  begin
    OwnerDocument := TXMLDocument.Create(nil);
    OwnerDocument.DOMDocument := MSDOMDocumentToDOMDocument(MSDOMNode.ownerDocument);
  end;
  Result := TXMLNode.Create(MSDOMNodeToDOMNode(MSDOMNode), nil, (OwnerDocument as IXMLDocumentAccess).DocumentObject);
end;

var
  Indent: Integer = 0;

procedure ShowNode(const Node: IXMLNode);
var
  I: Integer;
begin
  Inc(Indent);
  Writeln;
  Writeln(StringOfChar(' ', Indent * 2) + 'NodeName: ' + Node.NodeName);
  Writeln(StringOfChar(' ', Indent * 2) + 'NodeType: ' + NodeTypeNames[Node.NodeType]);
  for I := 0 to Node.AttributeNodes.Count - 1 do
    ShowNode(Node.AttributeNodes[I]);
  if Node.HasChildNodes then
    for I := 0 to Node.ChildNodes.Count - 1 do
      ShowNode(Node.ChildNodes[I])
  else
    Writeln(StringOfChar(' ', Indent * 2) + 'NodeValue: ' + Node.NodeValue);
  Dec(Indent);
end;

procedure Main;
var
  MSDOMDocument: IXMLDOMDocument;
  XMLDocument: IXMLDocument;
  MSDOMNode: IXMLDOMNode;
begin
  MSDOMDocument := CoDOMDocument.Create;
  MSDOMDocument.load('C:\Program Files\Embarcadero\RAD Studio\8.0\ObjRepos\en\Code_Templates\Delphi\blockcomment.xml');
  MSDOMNode := MSDOMDocument.documentElement;
  // show all
  ShowNode(MSDOMNodeToXMLNode(MSDOMNode, XMLDocument)); // you can reuse XMLDocument
  Writeln;
  // show author
  MSDOMNode := MSDOMDocument.selectSingleNode('/codetemplate/template/author');
  ShowNode(MSDOMNodeToXMLNode(MSDOMNode, XMLDocument));

  Readln;
end;


begin
  ReportMemoryLeaksOnShutdown := True;
  try
    CoInitialize(nil);
    try
      Main;
    finally
      CoUninitialize;
    end;
  except
    on E: Exception do
    begin
      ExitCode := 1;
      Writeln(Format('[%s] %s', [E.ClassName, E.Message]));
    end;
  end;
end.
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8148977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档