How to find node using NativeXml : Delphi

5.4k views Asked by At

i'm trying to use NativeXml, yes its more simple than xmldocument, but i still cannot find a solution to find node that have multiple name..

<item>
  <name>Manggo Salad</name>
  <feature>Good</feature>
  <feature>Excelent</feature>
  <feature>Nice Food</feature>
  <feature>love this</feature>
</item>

how to find "feature" ??

2

There are 2 answers

5
Uwe Raabe On BEST ANSWER

TXmlNode.FindNodes takes the NodeName and a TXmlNodeList/TsdNodeList which will be filled with all nodes matching this name. If you don't want it to be recursive use NodesByName.

0
menjaraz On

Here is another down to earth possible solution:

program NodeAccess;

{$APPTYPE CONSOLE}

(*

Assumption

<item>
  <name>Manggo Salad</name>
  <feature>Good</feature>
  <feature>Excelent</feature>
  <feature>Nice Food</feature>
  <feature>love this</feature>
</item>

as XML is stored in "sample.xml" file along with binary executable
of this program.

Purpose
  How to iterate multiple occurence of the "feature" node

*)

uses
  SysUtils,
  NativeXML;

const
  cXMLFileName = 'sample.xml';

var
  i,j: Integer;
  AXMLDoc: TNativeXml;

begin
  try
    Writeln('Sample iterating <',cXMLFileName,'>');
    Writeln;

    AXMLDoc := TNativeXml.Create(nil);

    try
      AXMLDoc.LoadFromFile(cXMLFileName);

      if Assigned(AXMLDoc.Root) then
        with AXMLDoc.Root do
          for i := 0 to NodeCount - 1 do
          begin
            if Nodes[i].Name='feature' then
              for j := 0 to Nodes[i].NodeCount - 1 do
                Writeln('  ',Nodes[i].Name, ' >>> ', Nodes[i].Nodes[j].Value)
          end;
    finally
      AXMLDoc.Free;
    end;

    Writeln;
    Writeln('Hit Return to quit');
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.