问题
I am currently using NativeXML in Delphi and I have this XML document with the following structure:
<?xml version="1.0"?>
<Request>
<RequestId>5429935816</RequestId>
<CompletedDate>2012-07-12T12:06:57+00:00</CompletedDate>
</Request>
<RequestId>
<RequestId>5428581330</RequestId>
<CompletedDate>2012-07-12T04:21:46+00:00</CompletedDate>
</Request>
Basically I need to know the value of each RequestID in the document.
Thank you in advance, and regards.
回答1:
Here is some code (not tested, written out of my memory..) which shows how to loop thru nodes... (Of course you have to replace the strBuf-thing and filename with some real code...)
procedure ReadNodes;
var
strBuf: string;
i: Integer;
begin
aXMLDoc := TNativeXML.Create;
try
aXMLDoc.ExternalEncoding := seUTF8; //for example...
aXMLDoc.LoadFromFile(FileName);
if assigned(aXMLDoc.Root) then
begin
for i := 0 to aXMLDoc.Root.NodeCount - 1 do
begin
strBuf := aXMLDoc.Root.Node[i].NodeByName('RequestID').ValueAsString;
end;
end;
finally
aXMLDoc.Free;
end;
end;
回答2:
Node.FindNodes('Request', AList);
for I := 0 to AList.Count - 1 do begin
Node2 := TXmlNode(AList[I]);
if Assigned(Node2.NodeByName('RequestId')) then begin
Node3 := Node2.NodeByName('RequestId');
s := Node3.ValueUnicode;
//...s
end;
end;
来源:https://stackoverflow.com/questions/11483436/how-do-i-iterate-through-similar-nodes-in-an-xml-document-using-nativexml-in-del