我尝试找到具有特定“节点名称”、“属性”和“属性值”的节点。我使用下面的递归函数。
我的 XMLDocument 有一个名为“TestNodeName”的节点,其属性为“Format”,值为“1”。
该函数第一次运行良好:返回更正确的节点。
当我第二次调用它时,它给出了错误的结果:返回一个具有值为 0 的 Format 属性的节点。
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'); 第一次调用 RecursiveFindNodeAttr 会给出好的结果,因为 "cnode:=" 第二次调用 RecursiveFindNodeAttr 会给出错误的结果 (ID=1) 因为我使用了相同的变量 "foundNode:=RecursiveFindNodeAttr(..."
最后,当我移动“var foundNode:IXMLNode;”时 从 Tform4 声明到全局,第二次调用返回良好结果 (ID=4)
我发现了另一个问题。当我在将所有格式=“1”替换为格式=“5”后在循环中使用 RecursiveFindNodeAttr 时,“foundNode”结果仍然是“非零”,因此循环永远不会结束。
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;