Delphi中的DOMElement

时间:2011-07-23 01:10:43

标签: delphi dom dom-node ixmldomelement ixmldomnode

我如何在DOMNodeList对象中使用.getElementsByTagName? 像:

procedure TForm1.selecionarClick(Sender: TObject);
var  DOMDocument: iXMLDOMDocument;
     DOMNodeList: iXMLDOMNodeList;
     DOMNode: iXMLDOMNode;
     DOMElement: iXMLDOMElement;
     i: Integer;
begin
      Memo.Text := '';
      with DOMDocument do
      begin
            DOMDocument := coDOMDocument.Create;
            DOMDocument.load( 'C:\Usuarios.xml' );
            DOMDocument.preserveWhiteSpace := false;
            DOMNodeList := DOMDocument.selectNodes( './/usuario[@codigo="'+codigo.Text+'"]/' );
            for i := 0 to DOMNodeList.length - 1 do
            begin

            end;
      end;
end;

我的XML结构:

<?xml version="1.0" encoding="utf-8"?>
<usuarios>
    <usuario codigo="1">
           <nome>Name Node</nome>
           <sobrenome>Last Name Node</sobrenome>
           <cidade>City Node</cidade>
           <estado>State Node</estado>
           <email>Mail Node</email>
    </usuario>
</usuarios>

2 个答案:

答案 0 :(得分:2)

GetElementsByTagName 不是 IXMLDOMNodeList 的成员,而是 IXMLDOMDocument 的成员。在IXMLDOMNodeList上,要按标记名称获取,必须使用这种类型的构造循环:

for i := 0 to DOMNodeList.length - 1 do
begin
  DOMNode := DOMNodeList[i];
  if DOMNode.nodeName = 'aTagName' then
   DoStuff(DOMNode);
  // etc etc....
end;

HTH

答案 1 :(得分:0)

IDOMElement支持getElementsByTagName,它返回一个IDOMNodeList。 IDOMElement是IDOMNode的“子类”。

var
 DOMNode: IDOMNode;
 DOMElement: IDOMElement;
begin
  if Node.DOMNode.nodeType <> ELEMENT_NODE then
    exit;

  // Obtain IDOMElement interface
  DOMElement := (DOMNode as IDOMElement);
  // Fetch node list
  DOMNodeList := DOMElement.getElementsByTagName('search text');

  // Do whatever with the list....
end;

希望有所帮助。 :)