开发者

DOMElement in Delphi

开发者 https://www.devze.com 2023-03-22 14:32 出处:网络
how i can use .getElementsByTagName in DOMNodeList Object ? Like: procedure TForm1.selecionarClick(Sender: TObject);

how i can use .getElementsByTagName in DOMNodeList Object ? Like:

procedure TForm1.selecionarClick(Sender: TObject);
var  DOMDocument: iXMLDOMDocument;
     DOMNodeList: iXMLDOMNodeLis开发者_如何转开发t;
     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;

My XML structure:

<?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>


GetElementsByTagName is not a member of IXMLDOMNodeList, but of IXMLDOMDocument. On IXMLDOMNodeList, to grab by tag name you must loop using this type of construct:

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

HTH


IDOMElement supports getElementsByTagName which returns an IDOMNodeList. IDOMElement is a "subclass" of 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;

Hope that helps. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消