verysimplexml icon indicating copy to clipboard operation
verysimplexml copied to clipboard

FindNodes function not recursive

Open ulutepe opened this issue 6 years ago • 0 comments

fix with this

function TXmlNodeList.FindNodes(const Name: String; NodeTypes: TXmlNodeTypes = [ntElement]): TXmlNodeList;
var
  Node: TXmlNode;
begin
  Result          := TXmlNodeList.Create(False);
  Result.Document := Document;
  try
    for Node in Self do
      if ((NodeTypes = []) or (Node.NodeType in NodeTypes)) and IsSame(Node.Name, Name) then
      begin
        Result.Parent := Node.Parent;
        Result.Add(Node);
      end;
    Result.Parent := NIL;
  except
    Result.Free;
    raise;
  end;
end;
function TXmlNode.Find(const Name: String; NodeTypes: TXmlNodeTypes = [ntElement]): TXmlNode;
var
  cur: TXmlNode;
begin
  Result := ChildNodes.Find(Name, NodeTypes);
  if not Assigned(Result) then
    if ChildNodes.Count > 0 then
    begin
      cur    := Self.ChildNodes[0];
      Result := cur.Find(Name, NodeTypes);
    end;
end;



function TXmlNode.Find(const Name, AttrName, AttrValue: String; NodeTypes: TXmlNodeTypes = [ntElement]): TXmlNode;
var
  cur: TXmlNode;
begin
  Result := ChildNodes.Find(Name, AttrName, AttrValue, NodeTypes);
  if not Assigned(Result) then
    if ChildNodes.Count > 0 then
    begin
      cur    := Self.ChildNodes[0];
      Result := cur.Find(Name, AttrName, AttrValue, NodeTypes);
    end;
end;



function TXmlNode.Find(const Name, AttrName: String; NodeTypes: TXmlNodeTypes = [ntElement]): TXmlNode;
var
  cur: TXmlNode;
begin
  Result := ChildNodes.Find(Name, AttrName, NodeTypes);
  if not Assigned(Result) then
    if ChildNodes.Count > 0 then
    begin
      cur    := Self.ChildNodes[0];
      Result := cur.Find(Name, AttrName, NodeTypes);
    end;
end;



function TXmlNode.FindNodes(const Name: String; NodeTypes: TXmlNodeTypes = [ntElement]): TXmlNodeList;
var
  cur: TXmlNode;
begin
  Result := ChildNodes.FindNodes(Name, NodeTypes);
  if Assigned(Result) then
    if Result.Count = 0 then
      if ChildNodes.Count > 0 then
      begin
        FreeAndNil(Result); // free because its created temp
        cur    := Self.ChildNodes[0];
        Result := cur.FindNodes(Name, NodeTypes);
      end;
end;

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

ulutepe avatar Nov 23 '19 12:11 ulutepe