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

蓝咒 提交于 2019-12-25 01:39:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!