2

我正在尝试使用NativeXml,是的,它比 xmldocument 更简单,但我仍然找不到找到具有多个名称的节点的解决方案..

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

如何找到“功能”?

4

2 回答 2

3

TXmlNode.FindNodes接受 NodeName 和 a TXmlNodeList/TsdNodeList,其中将填充与该名称匹配的所有节点。如果您不希望它是递归使用NodesByName.

于 2012-01-28T16:19:33.483 回答
1

这是另一个切实可行的解决方案:

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.
于 2012-01-28T17:24:48.097 回答