如何在Delphi中使用NativeXML迭代XML文档中的类似节点?

时间:2021-07-02 20:22:58

I am currently using NativeXML in Delphi and I have this XML document with the following structure:

我目前在Delphi中使用NativeXML,我有这个XML文档,结构如下:

<?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.

基本上我需要知道文档中每个RequestID的值。

Thank you in advance, and regards.

提前谢谢你,并致以问候。

2 个解决方案

#1


0  

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...)

这里有一些代码(没有测试,写出我的内存..),它显示了如何循环通过节点...(当然你必须用一些真正的代码替换strBuf-thing和filename ...)

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


0  

 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;  

#1


0  

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...)

这里有一些代码(没有测试,写出我的内存..),它显示了如何循环通过节点...(当然你必须用一些真正的代码替换strBuf-thing和filename ...)

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


0  

 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;