我试图使用NativeXml,是的,它比xmldocument更简单,但我仍然找不到找到具有多个名称的节点的解决方案..
<item>
<name>Manggo Salad</name>
<feature>Good</feature>
<feature>Excelent</feature>
<feature>Nice Food</feature>
<feature>love this</feature>
</item>
如何找到&#34;功能&#34; ??
答案 0 :(得分:3)
TXmlNode.FindNodes
获取NodeName和TXmlNodeList/TsdNodeList
,其中将填充与此名称匹配的所有节点。如果您不希望它是递归的,请使用NodesByName
。
答案 1 :(得分: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.