Search code examples
delphinativexml

How do I iterate through similar nodes in an XML document using NativeXML in Delphi?


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.


Solution

  • 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;