Delphi XML Recursive函数,搜索具有特定属性值的节点

时间:2013-07-08 13:43:48

标签: xml delphi recursion find

我尝试找到具有特定“节点名称”,“属性”和“属性值”的节点。我使用下面的递归函数。

我的XMLDocument有一个名为'TestNodeName'的节点,其属性为'Format',值为'1'。

第一次该功能正常工作:返回节点节点 当我第二次调用它时,它会给出错误的结果:返回一个Format属性值为0的节点。

XML示例。

<mnode>
  <TestNodeName ID="1" Format="0">
  </TestNodeName>
  <TestNodeName ID="2" Format="1">
  </TestNodeName>
  <TestNodeName ID="3" Format="0">
  </TestNodeName>
  <TestNodeName ID="4" Format="1">
  </TestNodeName>
  <TestNodeName ID="5" Format="0">
  </TestNodeName>
</mnode>

XML结束

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, XMLIntf, XMLDoc;

type
  TForm4 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
//GLOBAL VARIABLES
var
  Form4: TForm4;
  XML:IXMLDocument;
  mnode:IXMLNode;
  s:string;
implementation

{$R *.dfm}
function RecursiveFindNode(ANode: IXMLNode; const SearchNodeName: string): IXMLNode;
var
  I: Integer;
begin
  if CompareText(ANode.NodeName, SearchNodeName) = 0 then
    Result := ANode
  else if not Assigned(ANode.ChildNodes) then
    Result := nil
  else begin
    for I := 0 to ANode.ChildNodes.Count - 1 do
    begin
      Result := RecursiveFindNode(ANode.ChildNodes[I], SearchNodeName);
      if Assigned(Result) then
        Exit;
    end;
  end;
end;

function RecursiveFindNodeAttr(ANode: IXMLNode; const SearchNodeName: string; sAttr, sAttrVal:string): IXMLNode;
var
  I: Integer;
  sAttrFind: ixmlnode;
  stext:string;
begin
  sAttrFind:=ANode.AttributeNodes.FindNode(sAttr);
  if sAttrFind<>nil then stext:=sAttrFind.Text else stext:='';
  if (CompareText(ANode.NodeName, SearchNodeName)=0)and(CompareText(sAttrFind.NodeName, sAttr)=0)and(CompareText(stext, sAttrVal)=0) then
  begin
    Result := ANode;
  end
  else if not Assigned(ANode.ChildNodes) then
  begin
    Result := nil;
  end
  else begin
    for I := 0 to ANode.ChildNodes.Count - 1 do
    begin
      Result := RecursiveFindNodeAttr(ANode.ChildNodes[I], SearchNodeName, sAttr, sAttrVal);
      if Assigned(Result) then
      begin
        Exit;
      end;
    end;
  end;
end;

procedure TForm4.FormCreate(Sender: TObject);
var
cnode,foundNode:IXMLNode; //<-- Problem here "foundNode" must be in global 
begin
XML:= NewXMLDocument;
XML.LoadFromFile('C:\test.xml');
mnode:=XML.DocumentElement;
foundNode:=RecursiveFindNode(mnode,'TestNodeName');

//First time
cnode:=RecursiveFindNodeAttr(XML.DocumentElement,'TestNodeName','Format','1');
if cnode<>nil then
begin
cnode.Attributes['Format']:='5';
ShowMessage('ID='+cnode.Attributes['ID']);
end
else
ShowMessage('nil');

//Second time
foundNode:=RecursiveFindNodeAttr(XML.DocumentElement,'TestNodeName','Format','1');
if foundNode<>nil then
begin
foundNode.Attributes['Format']:='5';
ShowMessage('ID='+foundNode.Attributes['ID']);
end
else
ShowMessage('nil');

XML.SaveToFile('C:\test.xml');


end;

end.

经过几次测试后我终于找到了导致功能错误的原因。有一个类似的递归函数。当我删除功能调用时,所有结果都没问题。     RecursiveFindNode(ANode:IXMLNode; const SearchNodeName:string):IXMLNode;

在代码中,下面是呼叫     foundNode:= RecursiveFindNode(了mnode, 'TestNodeName'); 由于“cnode:=”,第一次调用RecursiveFindNodeAttr会得到很好的结果 第二次调用RecursiveFindNodeAttr将给出错误的结果(ID = 1),因为我使用了相同的变量“foundNode:= RecursiveFindNodeAttr(...”

最后当我移动“var foundNode:IXMLNode;”从Tform4声明到全局第二次调用返回了良好的结果(ID = 4)

我发现了另一个问题。当我在将所有format =“1”替换为format =“5”之后在循环中使用RecursiveFindNodeAttr时,“foundNode”结果仍为“not nil”,因此循环将永远不会结束。

foundNode:=RecursiveFindNodeAttr(XML.DocumentElement,'TestNodeName','Format','1');
while foundNode<>nil do
begin
    foundNode.Attributes['Format']:='5';
    ShowMessage('ID='+foundNode.Attributes['ID']);

foundNode:=RecursiveFindNodeAttr(XML.DocumentElement,'TestNodeName','Format','1');
if foundNode=nil then ShowMessage('nil');
end;

2 个答案:

答案 0 :(得分:1)

我发现已经制作的函数可以正常使用IXMLDocument。如果匹配参数,函数返回节点列表。 XML与上面相同(具有“UID”属性的那个)

//Declared funciton
function FindNodeList(xnRoot: IXmlNode; const nodePath: WideString): IXMLNodeList;
var
   intfSelect : IDomNodeSelect;
   intfAccess : IXmlNodeAccess;
   dnlResult  : IDomNodeList;
   intfDocAccess : IXmlDocumentAccess;
   doc: TXmlDocument;
   i : Integer;
   dn : IDomNode;
begin
   Result := nil;
   if not Assigned(xnRoot)
     or not Supports(xnRoot, IXmlNodeAccess, intfAccess)
     or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then
     Exit;

   dnlResult := intfSelect.selectNodes(nodePath);
   if Assigned(dnlResult) then
   begin
     Result := TXmlNodeList.Create(intfAccess.GetNodeObject, '', nil);
     if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then
       doc := intfDocAccess.DocumentObject
     else
       doc := nil;

     for i := 0 to dnlResult.length - 1 do
     begin
       dn := dnlResult.item[i];
       Result.Add(TXmlNode.Create(dn, nil, doc));
     end;
   end;
end;

var
  xlist:IXMLNodeList;
  mnode:IXMLNode;
  XML:IXMLDocument;
begin
  XML:= NewXMLDocument;
  XML.LoadFromFile('C:\test.xml');
  mnode:=XML.DocumentElement;

  //This will find all nodes in nodes and subnodes if node name is "TestNodeName" attribute Format="1"
  xlist:=FindNodeList(mnode,'//TestNodeName[@Format="1"]');
  for I := 0 to xlist.Count - 1 do
    begin
      //This will set Format value to "8"
      xlist[i].Attributes['Format']:='8';
    end;


end.

答案 1 :(得分:0)

似乎“if not Assigned(ANode.ChildNodes)then”总是被分配。当我把它改为“if not(ANode.HasChildNodes)然后”它永远停止循环。我添加到函数消息中以显示函数的执行方式。

它似乎检查了相同的节点2次,第一次在“if(CompareText(ANode.Node ...”和第二次循环中“I:= 0到ANode.ChildNo ...”。

<mnode UID="main">
  <TestNodeName UID="1" Format="5">
    <TestNodeName UID="11" Format="5">
    </TestNodeName>
    <TestNodeName UID="12" Format="5">
    </TestNodeName>
    <TestNodeName UID="13" Format="5">
    </TestNodeName>
  </TestNodeName>
  <TestNodeName UID="2" Format="5">
  </TestNodeName>
  <TestNodeName UID="3" Format="5">
    <TestNodeName UID="31" Format="5">
    </TestNodeName>
    <TestNodeName UID="32" Format="5">
      <TestNodeName UID="321" Format="1">
      </TestNodeName>
      <TestNodeName UID="322" Format="5">
      </TestNodeName>
      <TestNodeName UID="323" Format="1">
      </TestNodeName>
    </TestNodeName>
    <TestNodeName UID="33" Format="5">
    </TestNodeName>
  </TestNodeName>
  <TestNodeName UID="4" Format="5">
  </TestNodeName>
</mnode>

function RecursiveFindNodeAttr(ANode: IXMLNode; const SearchNodeName: string; sAttr, sAttrVal:string): IXMLNode;
var
  I: Integer;
begin
  if (CompareText(ANode.NodeName, SearchNodeName)=0)and(ANode.Attributes[sAttr]=sAttrVal) then
  begin
    ShowMessage('Found! '+ANode.Attributes['UID']);
    Result := ANode;
  end
  else
    begin
    ShowMessage('1st try='+ANode.Attributes['UID']);
    if not (ANode.HasChildNodes) then
    begin
    ShowMessage('nil');
    Result := nil;
    end
  else begin
    for I := 0 to ANode.ChildNodes.Count - 1 do
     begin
      Result := RecursiveFindNodeAttr(ANode.ChildNodes[I], SearchNodeName, sAttr, sAttrVal);
      ShowMessage('loop='+ANode.ChildNodes[I].Attributes['UID']);
      if Assigned(Result) then
      begin
        ShowMessage('found in loop='+ANode.ChildNodes[I].Attributes['UID']);
        Exit;
      end;
     end;
    end;
   end;
end;

var
  XML:IXMLDocument;
  mnode,cnode:IXMLNode;

begin

XML:= NewXMLDocument;
XML.LoadFromFile('C:\test.xml');
mnode:=XML.DocumentElement;

//This will replace all Format="1" to Format="0"
cnode:=RecursiveFindNodeAttr(mnode,'TestNodeName','Format','1');
while cnode<>nil do
begin
  cnode.Attributes['Format'] := '0';
  ShowMessage(cnode.Attributes['UID']);
  cnode:=RecursiveFindNodeAttr(mnode,'TestNodeName','Format','1');
end;
if cnode= nil  then
ShowMessage('cnode= nil ');

end.